63
$\begingroup$

I would like to know how are logarithms calculated by computers. The GNU C library, for example, uses a call to the fyl2x() assembler instruction, which means that logarithms are calculated directly from the hardware.

So the question is: what algorithm is used by computers to calculate logarithms?

  • 0
    [How does C compute sin() and other math functions?](https://stackoverflow.com/q/2284860/995714), [What algorithms do FPUs use to compute transcendental functions?](https://stackoverflow.com/q/13877303/995714)2018-08-14

3 Answers 3

35

It really depends on the CPU.

For intel IA64, apparently they use Taylor series combined with a table.

More info can be found here: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.24.5177

and here: http://www.computer.org/csdl/proceedings/arith/1999/0116/00/01160004.pdf

  • 0
    @naught101: The citeseer link seems to work (perhaps it is my work network which has access though). I can't access the second link too.2018-08-09
20

All methods I have seen reduce first by dividing by the power of $2$ in the exponent and adding that exponent times $\ln(2)$ to the result. From there, the two most common methods I have seen are Taylor series for $\ln(1+x)$ and a variant of the CORDIC algorithm.

J. M. also brings up Padé approximants which I have seen used in some calculator implementations.

  • 0
    J.S. Walther link was broken. Here is one that works (05/2016): http://ece-research.unm.edu/pollard/classes/walther.pdf2016-05-25
12

Read the docs and the source of the cephes library for instance. Try also these books:

See also https://stackoverflow.com/questions/2169641/where-to-find-algorithms-for-standard-math-functions.

  • 0
    I found a flaw in Hastings' polynomial approximation for Log. It has a discontinuity at the endpoint of the interval. If you are doing something that involves numerical differentiation, BAD NEWS. Before Hastings' 1955 book, there was a 1953 RAND report. He had been working on this problem since about 1948, maybe longer.2017-07-04