[lug] Slightly off: log function in Perl

D. Stimits stimits at idcomm.com
Sat Mar 31 00:08:47 MST 2001


Keith Herold wrote:
> 
> So, sorry about the post, but I am working on an assignment and I need
> to calculate the log base 10 of some frequency counts.  The problem is
> that the log function in Perl is a natural log, not an arbitrary base.
> Any ideas of where I can find something like this (CPAN doesn't seem to
> have any).
> 
> I would also like to avoid either a) switching languages, b) making a
> call to C's math library.
> 
> --Keith
> 

Try this...

(log base "b" of x) = (log base "n" of x)/(log base "n" of "b")

For example, if you have log base e of x, and want the log base 10 of x,
find the log base e of 10, and divide by it. If you set a constant to
the natural log of 10, you add a single division step by that constant;
else you take the natural log of 10 and then divide (the former is
faster, the latter will recompute to the limits of the machine and
library you run it on).

D. Stimits, stimits at idcomm.com



More information about the LUG mailing list