# How do computers calculate the log of a value? [duplicate]

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 GiraudoFeb 23 '14 at 13:12

• 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. – David Mitra Feb 23 '14 at 12:27
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.