2

I have a function $\frac{e^x - 1}{x}$. In order to avoid loss of significance when calculating values of this function near $x = 0$, I represent $e^x$ as Taylor series. The truncation error of $e^x$ is $\frac{x^5}{5!}*e^x$ (If i use 4 members of Taylor series for the approximation). But this is the error only for $e^x$, not for the whole function. How to compute truncation error for $\frac{e^x - 1}{x}$ when $e^x$ is computed using Taylor series approximation?

John
  • 1,313
  • 1
    You have$$e^x=1+x+\frac12x^2+\frac16x^3+\frac1{24}x^4\pm\frac1{120}x^5e^x$$Where $\pm$ denotes error. Thus,$$\frac{e^x-1}x=1+\frac12x+\frac16x^2+\frac1{24}x^3\pm\frac1{120}x^4e^x$$ – Simply Beautiful Art Sep 15 '17 at 23:54
  • But how did you expand the bottom function like that? Its first derivative is $(e^x - 1)'x - x'(e^x-1) / x^2$, never mind further derivatives – John Sep 15 '17 at 23:58
  • Ohhhh...i got it, ok :) thx. You could write this as an answer actually :) – John Sep 16 '17 at 00:00
  • Nah, I'm fine. You could write your own answer, elaborating on what I've done up above (admittedly not much) and I'll overlook it. – Simply Beautiful Art Sep 16 '17 at 00:07
  • In a computer program, (x==0)?1:expm1(x)/x or (1+x==1)?1:(exp(x)-1)/((1+x)-1) should give correct results for small x. – Lutz Lehmann Sep 16 '17 at 06:38
  • Btw, I think it should actually be$$\frac{x^5}{5!}e^{|x|}$$on that error term. Or,$$\frac{x^5}{5!}\max(e^x,1)$$ – Simply Beautiful Art Sep 16 '17 at 16:59
  • Yes, this is quite important - but why not $x^4$ in the nominator? Because in my problem set I also have a comment that this should be like your most current suggestion, but I dont understand why - we are assessing the error on the whole formula, and it means the error will be divided by $x$ too (like in the example you gave first and in the answer)? – John Sep 16 '17 at 17:51
  • @LutzLehmann when I saw your "(1+x==1)?1:(exp(x)-1)/((1+x)-1)" formula, I thought you'd found the holy grail. But try it on x=2.6301183453367e-13; the correct answer according to wolframalpha is 1.0000000000001315059172668465292041841285259436797400336217523976... but your formula gives 1.0008445945945945. That's worse than calculating (exp(x)-1)/x naively, which gives 1.00042211903768. Kahan's exp(x)==1?1:(exp(x)-1)/log(exp(x)) gives 1.0000000000001317, which is correct to double precision. – Don Hatch Aug 22 '22 at 08:44
  • @DonHatch : Yes, that trick is indeed misplaced at this point. The cause of the error is that in the exponential series $1+x+x^2/2+...=1+x·(1+x/2·(1+...))$ the additional terms to $1+x$ give a positive contribution, and under floating point rounding this leads to rounding up in the last mantissa bit. But as $x=2^{-42}+2^{-45}+2^{-47}+2^{-53}$, this results in a relative error of about $2^{-52}/2^{-42}=2^{-10}\approx 10^{-3}$, which is also about the error that you observed in the numerator. ... – Lutz Lehmann Aug 22 '22 at 10:41
  • Additionally, in (1+x)-1 the last bit is removed, giving a relative error of $-2^{11}\approx 0.5·10^{-3}$ in the denominator. // The expm1 expression does not have this problem, so it can be used with both branching variants. – Lutz Lehmann Aug 22 '22 at 10:41

1 Answers1

2

The Lagrange formula tells you that

$$e^x=\sum_{n=0}^N \frac{x^n}{n!} + \frac{x^{N+1}}{(N+1)!} e^{\xi_N(x)}$$

where $\xi_N(x)$ is between $0$ and $x$. You can then manipulate this expression directly to get

$$\frac{e^x-1}{x}=\sum_{n=0}^{N-1} \frac{x^n}{(n+1)!} + \frac{x^N}{(N+1)!} e^{\xi_N(x)}.$$

Ian
  • 101,645