A13 · Lesson 2 · Rounding error and cancellation
How a few ulps of error become a wrong answer
One rounding costs at most a part in . That sounds harmless, and it usually is, until two nearly equal numbers are subtracted. Their leading digits cancel, and what's left is mostly the rounding error they carried, now promoted to the front. The quadratic formula, the difference of two close voltages, for small x, a running sum of millions of samples: each can lose most of its digits this way. The cure is rarely more precision; it's rewriting the formula so it never subtracts nearly equal quantities.
- Both curves are , whose true value tends to . The solid one is computed as written, in double precision, live in your browser; the dashed one uses the identity .
- Move toward small x: around the solid curve starts to wobble, by it's wrong in the fourth digit, and below it's exactly 0, because rounds to exactly 1.
- The dashed curve stays at 0.5 all the way down: it never subtracts.
Predict
In double precision, what does give for the small root of , whose true value is ?
. The square root is , which rounds to the nearest double, (doubles near are apart). Subtracting from leaves just that rounding. The rewritten form adds instead of subtracting and gives .
Name it
Errors through arithmetic
If and :
- multiplying or dividing adds relative errors: harmless;
- adding numbers of one sign gives a relative error no worse than the larger;
- subtracting nearly equal numbers keeps the absolute error, , but divides by a small result: the relative error grows by about . That's cancellation, and it costs about decimal digits.
Rewrite to avoid it
| Instead of | Use |
|---|---|
| (with ) | |
| , for small x | math.expm1(x), math.log1p(x) |
Sums
Adding n numbers left to right has an error bound of about , because each partial sum is rounded. Adding 0.1 ten million times in double gives 999 999.9998389754. Kahan's compensated summation, or Python's math.fsum, carries the lost low-order part along and gives 1 000 000 exactly. (Since version 3.12, Python's built-in sum compensates too; a plain loop doesn't.)
Worked example
Two node voltages are 2.500 004 V and 2.500 001 V, each known to 1 µV. What's their difference, and to what relative accuracy?
Show the solution
- The difference.
- Its error. Each input can be off by 1 µV, so the difference can be off by up to 2 µV: a relative error of 67%, from inputs good to 0.4 parts per million.
- The amplification factor.
- The fix. The fix is in the measurement: measure the difference directly, with a differential amplifier.
Answer the difference is 3 µV, uncertain by up to 2 µV, a relative error of 67% from inputs good to 0.4 parts per million.
Worked example
Evaluate at in double.
Show the solution
- The direct formula. Doubles near are 2 apart, so rounds to : The formula gives exactly 0.
- The rewritten form. This is right to full precision.
Answer the formula as written gives exactly 0; the rewritten form gives , right to full precision.
Worked example
Compute at in double, and find a form that keeps all its digits.
Show the solution
- The cosine. , which rounds to a double whose last bits are uncertain at the level.
- Subtract from 1. The seventh digit is already wrong, an error of about relative.
- Use an identity with no subtraction. This is correct to the last digit.
- The rule. When a formula subtracts nearly equal quantities, look for an identity that does the cancellation algebraically.
Answer direct subtraction gives , wrong in the seventh digit; gives , correct to the last digit.
Check
a = 1 + 1 × 10⁻⁶ and b = 1 each carry a relative rounding error u. By about what factor is the relative error of larger than u?
Check
Two numbers agree to a relative difference of 1 × 10⁻⁶. About how many decimal digits does subtracting them cost?
Check
What's the small root of ? (Compute it without cancellation.)
Check
In a format with 53 significant bits, below about what x does round to exactly 1? (Use and the spacing just below 1.)
Check
A left-to-right double sum of 1000000 terms has = 1. What's the worst-case error bound ?
Check
Which form of is accurate for large x?
Check
Compute for x = 0.01 in double precision.
(a)
What is its true value?
(b)
About how many decimal digits does the direct subtraction lose?
Check
Rewrite expressions that cancel for large x.
Step 1
Write without a subtraction.
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
Write as a single logarithm.
Explain why
Why does subtracting two nearly equal numbers destroy accuracy, when each number is accurate to 16 digits?
Each number carries a small absolute error, about one part in of its size. Subtracting removes the leading digits the two numbers share, but it can't remove their errors; those stay the same absolute size. The result is much smaller than the inputs, so the same absolute error is now a much larger fraction of it. If the inputs agreed to 12 digits, only about 4 meaningful digits remain. Nothing was rounded badly in the subtraction itself; it just exposed the errors that were already there, which is why the cure is to rearrange the formula so the subtraction never happens.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.