# $e^{-5}$ error using Taylor's series

I am a student and I was reading Numerical Analysis by Burden. In one of the exercises, I have to calculate $e^{-5}$ in two ways.

The first is using the Taylor's series for $x=-5$, $( e^{-5} = 1 - 5/1! + 5^2/2!-...)$ and second way is calculating $e^5$ and then $e^{-5}=1/e^5$, again using Taylor's series but this time for $x=5$. In his solution, he says second way gives better results because we avoid subtraction. I think that it has to do with the floating point system but I am not very sure so I thought someone could further explain it to me. (Sorry if I have made any mistakes. English is not my native language)

• As Carl Bender famously points out, one of the worst things you can do with a series is sum it. – Pseudonym Jan 23 '17 at 1:00
• This is a pure mathematics question and should have been posted on or migrated to Mathematics. – Raphael Mar 12 '17 at 17:57
• $e^{-5}$ is not a good example, because you would get two plausible but different results. I'd choose a negative argument large enough so that the imprecise calculation gives a result outside [0, 1] where the result should be. – gnasher729 Jul 10 '17 at 22:13

## 2 Answers

Use a spreadsheet and calculate $e^{-100}$ both ways. It will become totally obvious what is going on. "We avoid subtraction" is not the problem. The problem is that you are adding the sum or difference of large numbers, and each large number has a large rounding error. For one method, the rounding error is not large compared to the result. For the other matter, the rounding errors are huge compared to the result.

Once you expand the series for

$$e^{(-x)}$$

you are going to get alternative negative terms as we can see the nth derivative of

$$e^{(-x)} = (-1)^n * e^{(-x)}$$

therefore the author advises to use the other method instead.

• I think the OP's difficulty is with understanding why subtraction can be a problem with floating point accuracy. Briefly: catastrophic cancellation. – j_random_hacker Jan 11 '17 at 14:58
• I think I forgot an important note , I will use Taylor's series for n=9 and even tho we have alternating series the numbers we subtract are not close too each other so as far as I know we wont have any rounding erros – van Jan 11 '17 at 15:00
• @j_random_hacker sorry for being so late didn't know i have to use @ to notify you ,my difficulty is what you say could you maybe explain me this catastrophic cancellation you mention, thanks a lot for your time – van Jan 11 '17 at 20:22
• @van: Basically, if you subtract 2 floating point numbers that are close enough together that their first, say, 10 bits are equal, then the result has 10 bits less precision than usual. For more detail I suggest googling "catastrophic cancellation" and "subtraction floating point accuracy". – j_random_hacker Jan 12 '17 at 9:51