Any of the Words
Link to us
Click to e-mail author
This code snippet returns the logarithmic value of a given number with 10 as its base (Common Logarithm)with out using the built-in VB LOG function(it uses e as its base[natural or napierian logarithm]).The accuracy of my LogC function is mostly upto 5 digits after decimal. For better accuracy you have to "INCREASE" the final value of the for loop-"J",and also have to "DECREASE" the increment value of variable Lg (Lg = Lg + 1/10 ^ x,where x is a positive integer). But this will result in the slow execution of the program.
Private Sub Form_Load() Command1.Caption = "&LogC" End Sub Private Sub Command1_Click() msgbox LogC(Val(InputBox("Enter a number:"))) End Sub Function LogC(Number as double) as double Dim Lg as Single If Number <= 0 Then Err.Raise 5, , "Logarithms for zero and negitive numbers are not defined." exit Function end if If 10 ^ (Len(Str(Int(Number)))-2)= Number then LogC = Len(Str(Int(Number)))-2 Exit Function Else Lg=Len(Str(Int(Number)))-2 For J= 1 to 99999 Lg= Lg + 0.00001 If 10 ^ Lg >= Number Then LogC = Lg Exit Function End If Next J End If End Function
© 2023 A1VBCode. All rights reserved.