Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2008-03-15 22:28:28

esatpllana
Member
Registered: 2007-11-22
Posts: 12

help me with this log code

can anyone comment this log function code

Function LogB(Number as double,Base as integer) 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
For I = 0 to Number
    If Base ^ I = Number then
       LogB=I
       Exit Function
    ElseIf Base ^ I < Number And Base ^ (I+1) > Number then
        Lg = I
        For J= 1 to 99999
            Lg = Lg + 0.00001 
            If Base ^ Lg >= Number Then
               LogB = Lg
               Exit Function
            End If
        NExt J
     End IF
Next I
End Function

I can't understand this how he build log function
byedunno


live your day
Mitrovica My City
tenth a class

Offline

#2 2008-03-16 04:38:31

LuisRodg
Real Member
Registered: 2007-10-23
Posts: 322

Re: help me with this log code

The function takes in the number in double and the base in integer such that when the function is called:

When we are trying to find the log of x to the base of b equals "I" then this is the same as:

and this is the same that this code is using.

First of all if the log number is negative, this code gives an error because logs are only defined for Number > 0.

If our number is nonnegative then the code proceeds and we enter a For loop from I = 0 to Number. The first check this For loop does is to see if Base ^ I = Number because if thats the case, it means that "I" is our answer to the log, therefore if thats the case then we just say that LogB=I and exit the function because we just found it.

If this is not the case then the For loop does another check and this check is to see if Base^I is less than the Number and Base^(I+1) is greater than the Number. The loop will continue looping until we find that I that makes Base^I less than Number and Base^(I+1) greater than Number. When we find this I value, this means that the answer to the log is between I and (I+1) and this is because of what we said before.

Remember that when we had a log, it was the same as this:

Base^I = Number

So in the code, if we have:

Base^I < Number
Base^(I+1) > Number

that tells us that the log is greater than I and less (I+1) so our answer is in the interval (I,I+1).

Ok, so now that we know our answer must be between I and (I+1) then the code creates another For loop which runs from 0 to 99999 and what this does is that it keeps adding 0.00001 to Lg (Lg = I) and keeps checking if Lg is our answer. We are bound to find it because we knew that our answer was between I and I+1 so if kept adding 0.00001 to I and checking, add another 0.00001 and check again, ..... , eventually we will find our answer to the log.

Last edited by LuisRodg (2008-03-16 04:40:33)

Offline

Board footer

Powered by FluxBB