On 06/05/06, M. Edward (Ed) Borasky <znmeb / cesmail.net> wrote:
[...]
> You have to compute the Multiplier *once*. My guess is that most
> implementations of other languages compute logs to only one base,
> probably the natural (base e) log, and compute logs to all other bases
> by doing this multiplication every time you call them -- or worse, not
> pre-computing the Multiplier and doing a divide every time, which takes
> longer and is less accurate.
[...]


Actually, no.

Most programming languages do not implement their own log functions,
they use those provided in libmath. The latter provides two distinct
functions for natural and base-two logs.
On intel processors, the FPU directly provides the instructions FYL2X
and FYL2XP1 [1] for logarithms, and, in case you're wondering, L2
stands for log2: the two instructions compute y * log2(x) and y *
log2(x+1), respectively, with x at the top of the FPU stack and y just
below.

When computing log2(x) you just push 1 and x on the FPU stack then do an FYL2X.
When computing log(x) you push log(2)=1/(log2(e)) (with the dedicated
instruction FLDLN2) and x on the FPU stack then do an FYL2X.
For an arbitrary base you have to pre-compute 1/(log2(x)) with FYL2X
as described above.

If your processor does not handle logarithms natively, libmath
implementations usually have two different implementations for log and
log2 to minimize rounding errors. See for instance
sysdeps/ieee754/dbl-64/e_log2.c and sysdeps/ieee754/dbl-64/e_log.c in
the GNU C library version 2.4 [2]. In sysdeps/i386/fpu/ you'll see the
Intel-specific implementations.


Ciao,
Stefano

[1] http://www.intel.com/design/Pentium4/manuals/253666.htm
[2] http://www.gnu.org/software/libc/libc.html