78

I would like to know how logarithms are 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?

zar
  • 4,158
  • 4
  • 26
  • 42

3 Answers3

38

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

Aryabhata
  • 79,315
  • 8
  • 179
  • 262
23

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.

robjohn
  • 326,069
  • 34
  • 421
  • 800
  • 1
    In [QuickDraw GX](http://en.wikipedia.org/wiki/QuickDraw_GX), I used the CORDIC algorithm to compute trigonometric and inverse trigonometric functions as well as logarithms and exponentials on processors without FPUs. – robjohn Sep 01 '11 at 18:07
  • 1
    I've seen at least one system use a Padé approximant instead of a Maclaurin series, but yes, I believe almost all implementations exploit $\log(ab)=\log\,a+\log\,b$ for range reduction... – J. M. ain't a mathematician Sep 01 '11 at 18:08
  • 2
    @J. M.: Padé approximations can be better than Maclaurin series for certain functions. However, usually, the number of terms in the numerator and denominator are about the same as the number of terms in an equivalent Maclaurin series. Then there is an extra division, which can be expensive on some processors. I generally avoided them, but it is definitely worth mentioning them. Thanks. – robjohn Sep 01 '11 at 18:14
  • 1
    Right, one really has to do testing if you're implementing from the bottom up. At least the OP now knows there's a lot to pick from. – J. M. ain't a mathematician Sep 01 '11 at 18:17
  • 5
    [A 1971 paper by J. S. Walther](http://www.jacques-laporte.org/Welther-Unified%20Algorithm.pdf) (PDF) describes a unified CORDIC algorithm that can be used for multiplication, division, sin, cos, tan, arctan, sinh, cosh, tanh, arctanh, ln, exp and square root. – oosterwal Sep 01 '11 at 20:11
  • @oosterwal: cool. This explains why I saw the CORDIC algorithm on HP calculators. – robjohn Sep 01 '11 at 20:41
  • @robjohn The main interest of Pade-Hermite approximant (computed for example from a continued fraction expansion) is that the convergence is much faster than Taylor series on the whole complex plane (not just the unit disk). – Lierre Jun 04 '14 at 12:52
  • 2
    J.S. Walther link was broken. Here is one that works (05/2016): http://ece-research.unm.edu/pollard/classes/walther.pdf – Paul Chernoch May 25 '16 at 19:51
15

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.

lhf
  • 208,399
  • 15
  • 224
  • 525
  • 2
    [Hastings's book](http://www.amazon.com/dp/0691079145) is an oldie-but-goodie. If the OP doesn't need that much accuracy, the approximations given there might be adequate. – J. M. ain't a mathematician Sep 01 '11 at 18:32
  • 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. – richard1941 Jul 04 '17 at 08:57