VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Finds the logarithim of a number without using the VB log() function

by Redsting71 (9 Submissions)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 17th July 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Finds the logarithim of a number without using the VB log() function

API Declarations


'Only uses multiplication, division, and exponents to calculate logarithims
'If you care how it works for the example 3 ^ x = 5 (log 5 / log 3, or log 5 base 3)
'In 3 ^ x = 5, it takes the first digit of 5.0000000 (5).
' 1) It sees that 3 ^ 1 is the closest digit to 5 below 5, 3 ^ 2 is higher than it (9 > 5, but 3 < 5)
' 2) It adds 1 to the answer. That is the first digit
' 3)It then gets the second number. It does [5 / (3 ^ 1)] ^ 10, which is rougly 165
' 1a)Then it sees that 3 ^ 4 is the closest number below 165, 4 being the exponent.
' 2a)It adds 4 to the answer, which is now 1.4
' 3a)Then it does [165 / (3 ^ 4)] ^ 10, which is about 2.03. This is about 1188.
' 1b)Now it sees 3 ^ 6 is the lowest number below 1188, 3 ^ 7 is above it. So, it adds 6 to the answer.
' 2b)The answer is now 1.46.
' 3c)Then it does [1188 / (3 ^ 6)] ^ 10, and the process continues.
' .
' .
' .
'END: It solves it for the first 16 digits. The answer is 1.4649735207179, as is given

Rate Finds the logarithim of a number without using the VB log() function



xres$ = " "
Y = 1 
If n2 = 0 Then Exit Sub 
If n1 = 1 And n2 <> 1 Then Exit Sub 
If n2 < 0 And n1 > 0 Then Exit Sub
f = 0
b1 = n1
b2 = n2
go:
f = f + 1 
a1 = n1 ^ Y 
a2 = n1 ^ (Y + 1) 
If a1 = n2 Then xres$ = Str(Y): GoTo Done 
If n2 = 1 And n1 <> 0 Then xres$ = Str(0): GoTo Done 
If a2 = n2 Then xres$ = Str(Y + 1): GoTo Done 
If f = 1000 Then GoTo Done
If a1 < n2 And n2 < a2 Then 
    xres$ = xres$ + Str(Y)   
    getval n1 ^ Y, n2 
    n2 = trueanswer 
    GoTo go 
End If
If a2 < n2 Then Y = Y + 1: GoTo go 
If a1 > n2 Then Y = Y - 1: GoTo go
Done:
xres$ = Left(xres$, 32) 
d = Len(xres$) - 1 
If d = 1 Then GoTo AlmostDone 
xres$ = Val(xres$) / (10 ^ d) 
AlmostDone:
final$ = Val(xres$) 
xres$ = Val(final$) 
finished:
If b1 ^ 10 < b2 Then xres$ = Val(xres$) * 10 
If b1 ^ 100 < b2 Then xres$ = Val(xres$) * 100 
getlog = xres$
End Sub

Public Sub getval(Number1, Number2)
answer = Number2 / Number1
trueanswer = answer ^ 10 
End Sub

Download this snippet    Add to My Saved Code

Finds the logarithim of a number without using the VB log() function Comments

No comments have been posted about Finds the logarithim of a number without using the VB log() function. Why not be the first to post a comment about Finds the logarithim of a number without using the VB log() function.

Post your comment

Subject:
Message:
0/1000 characters