PDA

View Full Version : Antilogarithm or inverse logarithm in thinBasic



ErosOlmi
10-01-2011, 13:59
I've got a request via web site contact form about the possibility to have Antilogarithm or inverse logarithm in thinBasic.

Does someone can give me documentation links about this and/or help to build a native Math module new functions.

Thanks a lot
Eros

Petr Schreiber
10-01-2011, 14:23
Hi Eros,

this operation is quite easy in the end, description here:
http://mathworld.wolfram.com/Antilogarithm.html

The code for it could look like:


' -- General purpose
Function AntiLogarithm(nBase As Ext, nValue As Ext) As Ext

Return nBase^nValue

End Function

Function Logarithm(nBase As Ext, nValue As Ext) As Ext

Return Log10(nValue)/Log10(nBase)

End Function

' -- Or to directly map to ThinBASIC LOG10, LOG2 and LOG (called LN on calculator)
Function AntiLog10(nValue As Ext) As Ext

Return 10^nValue

End Function

Function AntiLog2(nValue As Ext) As Ext

Return 2^nValue

End Function

Function AntiLog(nValue As Ext) As Ext

Return M_E^nValue

End Function

Petr

Petr Schreiber
10-01-2011, 14:26
Here test sample:

Uses "Console"

Dim result As Number

result = Logarithm(3, 243)
PrintL "LOG3(243) = ", result
PrintL "AntiLOG3("+Format$(result)+") = ", AntiLogarithm(3, result)
PrintL

result = Log2(64)
PrintL "LOG2(64) = ", result
PrintL "AntiLOG2("+Format$(result)+") = ", AntiLogarithm(2, result), "=", AntiLog2(result)
PrintL

result = Log10(1000)
PrintL "LOG10(1000) = ", result
PrintL "AntiLOG2("+Format$(result)+") = ", AntiLogarithm(10, result), "=", AntiLog10(result)
PrintL

result = Log(M_E^2)
PrintL "LOGe(e^2) = ", result
PrintL "AntiLOGe("+Format$(result)+") = ", AntiLogarithm(M_E, result), "=", AntiLog(result)
PrintL

WaitKey


' -- General purpose
Function AntiLogarithm(nBase As Ext, nValue As Ext) As Ext

Return nBase^nValue

End Function

Function Logarithm(nBase As Ext, nValue As Ext) As Ext

Return Log10(nValue)/Log10(nBase)

End Function

' -- Or to directly map to ThinBASIC LOG10, LOG2 and LOG (called LN on calculator)
Function AntiLog10(nValue As Ext) As Ext

Return 10^nValue

End Function

Function AntiLog2(nValue As Ext) As Ext

Return 2^nValue

End Function

Function AntiLog(nValue As Ext) As Ext

Return M_E^nValue

End Function



Petr

Petr Schreiber
10-01-2011, 14:37
Code in first reply was not correct, now fixed.


Petr

ErosOlmi
10-01-2011, 15:14
Thanks a lot Petr, I will add to math arsenal!

Charles Pegge
10-01-2011, 16:53
Eros, Petr,

AntiLog is quite an old idea from the days when we used log tables and antilog tables. You already have exp, exp2 and exp10. These are the exact equivalents of antilogs,

so you could make antilog a synonym of exp or just persuade users to adopt exp :)

Charles

Petr Schreiber
10-01-2011, 17:58
Hi Charles,

of course you are right :p

Maybe at least the general purpose functions could have some use...

Logarithm could be implemented as LOG(base, number) and AntiLogarithm as EXP(base, value)... as thinBASIC can parse optional number of params.


Petr

Johannes
11-01-2011, 13:58
Also, you only need a single logarithm and exponentation function, ideally ln (natural logarithm) and exp (exponentation of e). I agree that log2 and log10 are very useful as they will be used regularly, but other bases are not necessary.

I also think that someone who needs base-3 logarithms (or base-pi for that matter) will be familiar with basic logarithmic rules.

logA b = ln b / ln a = Log(b) / Log(a)

a^b = e^(b ln a) = Exp(b * Log(a))