Taylor series convergence for $\log(x)$ I am trying to approximate $\log(x)$ using the Taylor's series expansion in software. It works mostly but for really small values of $x$ I need a large number of terms. For instance, with $x = 0.0001$, my estimate is close to $-4.0$ with 10 terms while actual correct answer is closer to $-8.0$.
I do need to actually implement this in an approximate closed form that is differentiable, hence I cannot use other software implementations. Is there a better approximation possible than Taylor's, or can I rewrite $\log(x)$ in a different form whose Taylor's expansion has lesser error?
 A: It depends on what values of $x$ you need an approximation for. Since you only mention very small but not very large values of $x$ I'll assume you want $x \in (0, 1)$ and $x$ is hopefully not too small. We can apply the following trick to speed up convergence: we have
$$\frac{1}{2} \ln \frac{1 + t}{1 - t} = \sum_{k \ge 0} \frac{t^{2k+1}}{2k+1}$$
(the Taylor series of hyperbolic arctangent). Now set $x = \frac{1 + t}{1 - t}$; solving for $t$ gives $t = \frac{x - 1}{x + 1}$, which gives
$$\boxed{ \ln x = 2 \sum_{k \ge 0} \frac{1}{2k+1} \left( \frac{x-1}{x+1} \right)^{2k+1} }.$$
This series converges very quickly for values of $x$ not too far away from $1$, for example for $x = 2$ we get a series for $\ln 2$ whose terms decay like $\frac{1}{9^k}$ whereas the usual Taylor series has terms that decay like $\frac{1}{k}$. Its terms are also invariant (up to sign) under the symmetry $\ln \frac{1}{x} = - \ln x$ so e.g. the series and hence the convergence for $x = \frac{1}{2}$ is the same as for $x = 2$. Here's a plot from WolframAlpha showing how accurate the very first term $2 \frac{x - 1}{x + 1}$ is as an approximation for $x \in (\frac{1}{10}, 10)$:

This still doesn't do so well for $x = 10^{-4}$ but for values of $x$ this far away from $1$ you really should be doing something else to reduce the computation to values of $x$ closer to $1$. An easy option for doing so is that if the values of $x$ you care about are all close (in the logarithmic sense) to some value $a$ (say, if $x \in [10^{-2}, 10^{-4}]$ you can take $a = 10^{-3}$) then you can compute
$$\ln x = \ln a + \ln \frac{x}{a}$$
so you just store the value of $\ln a$ and then use the Taylor series for $\frac{x}{a}$ which will be closer to $1$.
In fact it wouldn't be a bad idea to take $a$ to be, say, the largest power of $2$ smaller than $x$; then you'd only have to store the value of $\ln 2$ and repeatedly divide $x$ by $2$ (very easy in binary!), then evaluate $\ln x$ for $x \in [1, 2)$. If you take a fixed number of terms of the hyperbolic series the resulting approximation would be differentiable almost everywhere except at powers of $2$.
Let's see how this works for $x = 10^4$ (which, using the above series, is pretty much the same computation as $x = 10^{-4}$). The largest power of $2$ less than $x$ is $2^{13} = 8192$, which gives
$$\ln 10^4 = 13 \ln 2 + \ln \frac{10^4}{2^{13}}.$$
Using the first two terms of the series for $\ln 2$ and the first term of the series for the other log we get
$$13 \ln 2 \approx 26 \left( \frac{1}{3} + \frac{1}{3 \cdot 3^3} \right) \approx 8.99 \dots$$
and
$$\ln \frac{10^4}{2^{13}} \approx 2 \left( \frac{ \frac{10^4}{2^{13}} - 1}{ \frac{10^4}{2^{13}} + 1} \right) \approx 0.199 \dots $$
whose sum is approximately $9.19 \dots$. The true value is $9.21 \dots$ so we have accuracy to almost $2$ digits and we barely used any terms!
Truncating the hyperbolic series above produces approximations to $\ln x$ which are rational functions of $x$ rather than polynomials; for even better rational approximations you can try to compute the Padé approximants.
A: Since you're implementing this in software, there's a simple shortcut you can take.
See, float and double (if they follow the IEEE 754 standard) are stored in a form of scientific notation: $x = s2^pg$, with three fields for the sign bit, exponent, and significand.  Many programming languages have a function called frexp that will split a number into exponent and (signed) significand for you.
Then you can just use the identity $\log(2^pg) = p \log(2) + \log(g)$, treat $\log(2) \approx 0.6931471805599453$ as a constant, and reduce the problem to approximating $\log(g)$ for $g \in [0.5, 1)$.

And while you could use a Taylor series (based on the endpoint $x=0.5$ or $x = 1$, or on the midpoint $0.75$), it may make more sense to approximate $\log$ by a function that minimizes the error on the entire interval $[0.5, 1]$.  For example, to use least-squares error, find a function $f$ that minimizes
$$E := \int_{1/2}^1 (f(x) - \log(x))^2 ~dx$$
Using a simple quadratic polynomial $f(x) = ax^2 + bx + c$, the least-squares approximation (with coefficients to 6 decimal places) is:
$$f(x) = -0.934175x^2 + 2.765737x - 1.836220$$
This approximation has a root-mean-square error of 0.002033 and a maximum absolute error of 0.006252.
For greater accuracy, try generalizing $f$ to a higher-degree polynomial or a rational function.
