Logarithms to arbitrary base

The mathematics library in many languages ( e.g. C++) only provides us with natural (base e) logarithms and base 10 logarithms.

When programming, we sometimes need to perform logarithmic calculations that use an arbirary base. It is common, for instance, to need base 2 logarithms in order to work with sizes of binary trees, or base 8 logarithms for octrees.

The logarithm to an arbitrary base b, logb(x), can be computed from the logarithms of x and b with respect to an arbitrary base k using the following formula:

log_b(x)=log_k(x)/log_k(b)

where k could be ‘e’ or ’10′, which allows us to calculate logarithms to an arbitrary base using the standard maths library functions.