back

by dmitrygr·24d ago·view on hn ↗

  > you can compute any logarithm from two base-e ones.
They need not be base e. Any base will do as long as it is the same for both the numerator and the denominator
1 comments
Yeah, I simplified it a little. Though I must say I'm surprised pretty much every library I looked at uses the natural logarithm specifically, and not log2, which would seemingly be easier to compute with floats. Does anyone here know why, by any chance?
You can change base with a single multiplication by special constants if you accept 1ULP error, so working in one or the other doesn't really gain you much. But exp/logs are often implemented as a combination of a table lookup and polys, so you can just bake it into the constants even more easily.
Not sure why they wouldn't use log2 internally, but the mathematical reason is that e^x is the function whose derivative is itself, so it and the natural logarithm are fundamental to lots of other theories/operations like Euler's formula.
The only advantage of the hyperbolic logarithm (a.k.a. natural logarithm) is that the multiplicative factor is unity when computing its derivative or primitive.

The binary logarithm can be computed faster and more accurately, except for arguments very close to 1.

It is a historical accident that most standard libraries have been defined in the past to include natural logarithms instead of binary logarithms. Moreover lazy implementers sometimes have written a binary logarithm function that uses a pre-existing hyperbolic logarithm function, instead of the direct implementation that would be much more efficient.

Derivatives and primitives are seldom computed, but the computational advantage of binary logarithms applies to each function evaluation.

Even when derivatives or primitives are computed, in almost all applications there already exists another multiplicative constant in the formula that must be computed, which can absorb in it the factor "ln 2", so there is no increased computational cost.

There exists absolutely no reason to ever use multiple bases with logarithms.

One can choose to always use only binary logarithms and there exists no problem that would need other kinds of logarithms.

Choosing a base for logarithms is equivalent with choosing a unit of measurement for them. Changing the base of logarithms is done by the same rules as changing the unit of measurement for any quantity. In the same way as there is no need to use multiple units of measurement for a quantity, there is no need to ever work with multiple bases for logarithms.

Thus there is no reason for a programming language to include a function with 2 arguments like "log(arg, base)" instead of having only a function with 1 argument, e.g. "log2" (and the 2 constants LN_2 and LOG2_E).