What is a simple way to compute $10^x - 1$, where $x$ is close to zero?
Using exponentiation and then subtraction isn't good enough, because the fractional part is very small compared to the one that we subtract away.
Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. It only takes a minute to sign up.
Sign up to join this communityWhat is a simple way to compute $10^x - 1$, where $x$ is close to zero?
Using exponentiation and then subtraction isn't good enough, because the fractional part is very small compared to the one that we subtract away.
Use the Taylor series expansion of $a^x$ about $x=0$ and evaluate a small number of terms for $a=10$:
$$a^x - 1 \approx x\log(a) + \frac{1}{2}x^2\log^2(a)+\cdots.$$
Personally, in the absence of a special function like expm1
(see also this scicomp.SE question), I'd use a Padé approximant instead of a Taylor/Maclaurin series; usually, those have a slightly wider applicability for the same amount of computational effort.
I'll be discussing $\exp x-1$ for the rest of the answer, since $10^x-1=\exp(x\log 10)-1$.
To illustrate, here's a plot comparing the relative error of the $7$-th order Taylor polynomial
$$x+\frac{x^2}{2}+\frac{x^3}{6}+\frac{x^4}{24}+\frac{x^5}{120}+\frac{x^6}{720}+\frac{x^7}{5040}$$
and the $(4,3)$ Padé approximant
$$\frac{x+\frac{x^2}{14}+\frac{x^3}{42}+\frac{x^4}{840}}{1-\frac{3 x}{7}+\frac{x^2}{14}-\frac{x^3}{210}}$$
Similar plots can be seen for order $2p+1$ Taylor polynomials vs. $(p+1,p)$ Padé approximants.
Another alternative presents itself, if your system has the $\sinh$ function: use the relation
$\exp x-1=2\exp\left(\frac{x}{2}\right)\sinh\left(\frac{x}{2}\right)$
Finally, Higham presents a neat trick in Accuracy and Stability of Numerical Algorithms (second edition, p. 19), and originally attributed to Kahan. Look there for more details.
exp10m1
, which is a recommend operation per IEEE-754 (2008) and provides exactly what you need. $\endgroup$ – njuffa Mar 24 '17 at 2:21