How to compute $(e^x -1)/x$ when $-10^{-8}<x<10^{-8}$

187 Views Asked by At

I am trying to graph the function $y=(e^x -1)/x$ for values of $x$ close to zero.

The result should be $y$ close to $1$. Howevere, both in Excel and WolframAlpha the calculated values become very unpredictable for magnitudes of $x$ of the order $-10^{-8}<x<10^{-8}$.

(1) Why is this? (2) Is there a more accurate way to calculate this function when $x$ is close to zero?

3

There are 3 best solutions below

0
On BEST ANSWER

When $|x| \ll 1, e^x \approx 1+x+x^2/2$ from the Taylor series. When you subtract $e^x-1$ in a computer, you lose precision. Say your computer stores numbers in decimal with $10$ places (it works the same in binary, but we are used to decimal). In the computer, $e^{10^{-8}} = 1.000000010,$, so $e^{10^{-8}}-1 = 0.000000010$ and only has two places accuracy. For $|x| \ll 1$,it is much more accurate to analytically say $e^x-1 \approx x+x^2/2,$ so $(e^x-1)/x \approx 1+x/2$

This subject is discussed at great length in numerical analysis classes.

0
On

For that very reason, many math libraries (for example the standard POSIX libc) have a expm1 function giving a numerically accurate value for $e^x - 1$ when $x$ is small.

See for example this.

0
On

Further to the other answers, Wikipedia reports the following high-precision value, for small values of $x$, on systems where the function $expm1(x)$ is not available:-

$$ e^x-1 = \frac{2\tanh(x/2)}{1-\tanh(x/2)} .$$