This question already has an answer here:

I'm not sure if this question belongs on StackOverflow or here (please let me know if the former, and i'll delete this and ask there), but I was wondering how the log or ln of a value is calculated computationally with accuracy? Is some series implemented that approximates a value?

I looked into the Taylor series for the natural logarithm, but that is apparently only accurate for 0 < x < 2, and I can't find anything else. I tried looking for source code for Java's Math#log function as well to see what algorithm they implemented, but couldn't find any since it's implemented in a native language rather than in Java.


marked as duplicate by Henry, Henning Makholm, ShreevatsaR, Yiorgos S. Smyrlis, Davide Giraudo Feb 23 '14 at 13:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ The first link in the "related section" seems to answer this. $\endgroup$ – David Mitra Feb 23 '14 at 12:23
  • 1
    $\begingroup$ But, to address your concern in the second paragraph: choose $n$ so that $x/2^n<2$. Then compute $\log(x/2^n)$. You can then find $\log x$ by $\log(x/2^n)=\log x- n\log (1/2)$. You might even choose $n$ larger to obtain better and quicker approximations. $\endgroup$ – David Mitra Feb 23 '14 at 12:27
  • $\begingroup$ Thank you! I don't know why I didn't think of that earlier. I was looking for an algorithm to calculate this so that I can implement it myself, and this is perfect. $\endgroup$ – u3l Feb 23 '14 at 12:30

If the computer stores a floating point number as $a \times 2^b$ for $1 \le a \lt 2$ and $b$ an integer then $\log_e (a \times 2^b) = \log_e (a) + b \times \log_e(2)$.

For example you can use the Taylor series for $\log_e (1+x)$ to find $\log_e (a)$ and can store $\log_e(2)$ as a constant.


Not the answer you're looking for? Browse other questions tagged or ask your own question.