Large relative error

When evaluating $f(x)=\left(\dfrac{e^x-1-x}{x^2}\right)$, we observe large relative error as $x \rightarrow 0$.

Find a method to evaluate $f(x)$ to full machine precision for all $|x|<1$.

Solutions Collecting From Web of "Large relative error"

  • Calculate the quantities $$\left\{ \frac{1}{2!}, \frac{x}{3!} , \frac{x^2}{4!} \cdots \right\}$$ by starting with $\frac12$ and repeatedly multiplying by $x$ and dividing by the next integer. Terminate this when the last value is less than $0.18$ times the machine precision. ($0.18$ because that is less than the machine $\frac12$ times the machine times the smallest possible actual result).

  • Add these quantities in reverse order (to avoid losing precision in the smaller terms).

Find the minimum value of $N$ such that

$$\frac1{(N+3)!} < \epsilon$$

where $\epsilon$ is machine precision (typically about $10^{-16}$ at double precision). The reason this works is that the remainder term in the Taylor expansion about $x=0$ is about

$$\frac{x^{N+1}}{(N+3)!} $$

and the OP is interested in all $|x| \lt 1$.