A132 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 1016. 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, 1−cos⁡x 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 (1−cos⁡x)/x2, whose true value tends to 12. The solid one is computed as written, in double precision, live in your browser; the dashed one uses the identity 1−cos⁡x=2sin2⁡(x/2).
  • Move toward small x: around 10−5 the solid curve starts to wobble, by 10−7 it's wrong in the fourth digit, and below 10−8 it's exactly 0, because cos⁡x rounds to exactly 1.
  • The dashed curve stays at 0.5 all the way down: it never subtracts.

Predict

In double precision, what does (108−1016−4)/2 give for the small root of x2−108x+1=0, whose true value is 10−8?

Name it

Errors through arithmetic

If a^=a(1+δa) and b^=b(1+δb):

  • 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, |aδa−bδb|, but divides by a small result: the relative error grows by about |a|+|b||a−b|. That's cancellation, and it costs about log10⁡|a||a−b| decimal digits.

Rewrite to avoid it

Instead ofUse
1−cos⁡x2sin2⁡(x/2)
x+1−x1x+1+x
−b+b2−4ac2a (with b>0)2c−b−b2−4ac
ex−1, ln⁡(1+x) for small xmath.expm1(x), math.log1p(x)

Sums

Adding n numbers left to right has an error bound of about (n−1)u∑|xi|, 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
  1. The difference. ΔV=2.500004−2.500001V=3µV
  2. Its error. Each input can be off by 1 µV, so the difference can be off by up to 2 µV: 2µV3µV=67% a relative error of 67%, from inputs good to 0.4 parts per million.
  3. The amplification factor. 2.5+2.53×10−6≈1.7×106
  4. 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 x2+1−x at x=108 in double.

Show the solution
  1. The direct formula. Doubles near 1016 are 2 apart, so x2+1=1016+1 rounds to 1016: 1016−108=0 The formula gives exactly 0.
  2. The rewritten form. 1x2+1+x=12×108=5×10−9 This is right to full precision.

Answer the formula as written gives exactly 0; the rewritten form gives 5×10−9, right to full precision.

Worked example

Compute 1−cos⁡x at x=10−5 in double, and find a form that keeps all its digits.

Show the solution
  1. The cosine. cos⁡10−5=0.99999999995, which rounds to a double whose last bits are uncertain at the 10−16 level.
  2. Subtract from 1. 1−cos⁡x=5.0000004×10−11true=5.0000000×10−11 The seventh digit is already wrong, an error of about 10−8 relative.
  3. Use an identity with no subtraction. 1−cos⁡x=2sin2⁡x2=4.99999999996×10−11 This is correct to the last digit.
  4. The rule. When a formula subtracts nearly equal quantities, look for an identity that does the cancellation algebraically.

Answer direct subtraction gives 5.0000004×10−11, wrong in the seventh digit; 2sin2⁡x2 gives 4.99999999996×10−11, 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 a−b 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 x2−1 × 10⁸x+1=0? (Compute it without cancellation.)

Check

In a format with 53 significant bits, below about what x does cos⁡x round to exactly 1? (Use cos⁡x≈1−x2/2 and the spacing 2−p just below 1.)

Check

A left-to-right double sum of 1000000 terms has ∑|xi| = 1. What's the worst-case error bound (n−1)u∑|xi|?

Check

Which form of x2+1−x is accurate for large x?

Check

Compute 1−cos⁡x 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 x+1−x 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°.

Explain why

Why does subtracting two nearly equal numbers destroy accuracy, when each number is accurate to 16 digits?

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.