Section C.2 Catastrophic cancellation
ΒΆRecall that ifExample C.2.0.1.
We use the quadratic equation in the exact order indicated by the parentheses in
truncating every expression within square brackets to three significant digits, to solve
Now, if you do this to the full precision of a typical calculator, the answer is instead approximately β0.040064. The relative error we incurred is, approximately, 0.01/0.04=0.25.
What is going on here? The problem comes from the fact that there is error in the 24.9 that is encountered after the square root is taken. Since that number is close in magnitude, but of opposite sign to the β25 to which it is added, the result of β25+24.9 is mostly error.
This is known as catastrophic cancelation: adding two nearly equal numbers of opposite sign, at least one of which has some error in it related to roundoff, yields a result with large relative error.
Now, one can use an alternative formula to compute the root:
which yields
Carrying out the computations, rounding intermediate results, yields β.0401. The relative error is now 0.00004/0.040064β.001. It avoids catastrophic cancellation because now the two numbers of nearly equal magnitude are added instead.
Remark C.2.0.2.
The point is: if possible, avoid creating small intermediate results that amplify into a large relative error in the final result.