🎯Computer Arithmetic and Error
Rounding Error in Arithmetic
Error of one operation, accumulation, catastrophic cancellation and compensated summation
Vancouver, January 1982
The Vancouver Stock Exchange launches a new index of its listed stocks, starting at 1000.000. It is recalculated after every trade — thousands of times a day — and each time the result is stored with three decimal places.
Not rounded to three decimals. Truncated. The fourth decimal and everything after it is simply dropped.
Each truncation loses less than 0.001 points; on average about 0.0005. Nobody looks at the fourth decimal of a stock index.
By November 1983, twenty-two months later, the index stands at 524.811. The market has not crashed. When consultants recompute the index from the same trades with correct rounding, the answer is 1098.892.
Half the value of the index was lost one ten-thousandth of a point at a time. No single operation was badly wrong.
The last lesson modelled storage: . IEEE 754 makes the same promise for every basic operation. For and also , the computed result is the exact result, correctly rounded:
The standard model of floating-point arithmetic. Each operation adds at most one rounding error of relative size u — not more.
One operation is therefore never the problem. Two things are.
Accumulation. A computation with operations can carry rounding errors. If they have random signs they partly cancel, and the total typically grows like . If they all have the same sign — truncation, or a biased algorithm — they grow like . The Vancouver index did about a million truncations; a million times 0.0005 is about 500 points.
Cancellation. The model bounds the error of a subtraction relative to its result. When and are close, is tiny, and the errors and already carried — from storage, measurement or earlier operations — are now huge relative to the difference.
Derivation
How rounding errors pile up in a sum
Add three numbers in the order , applying the standard model to each addition, with its own rounding error or :
Multiply out and drop the products , which are of size and far too small to matter:
The first numbers added carry two rounding errors, the last only one. With terms the pattern continues: and pass through roundings, through , and so on, so
That is the worst case, when every has the same sign — as truncation guarantees. Rounding to nearest gives s of both signs, roughly independent of each other. Independent errors add like a random walk: their variances add, so errors of typical size give a total of typical size . A million operations: about for a biased rule, for an unbiased one. That factor of a thousand is the whole difference between the Vancouver index and a correctly rounded one.
💡Catastrophic cancellation
Subtracting two numbers that agree in their first significant digits loses about digits of relative accuracy.
The subtraction itself is exact (Sterbenz's lemma: if , then is computed without error). What it does is expose the errors already sitting in and . If each was good to 16 digits and they agree in the first 12, the difference has at most 4 good digits.
The cure is never more precision first. It is algebra: rewrite the formula so the subtraction disappears.
Derivation
Why agreeing digits are lost digits
Suppose the stored values already carry small relative errors: and , with . Subtract exactly:
The second term is the error. Relative to the true difference it is
Nothing in this bound depends on how the subtraction was done; the amplification factor belongs to the numbers. If and agree in their first significant digits, then and the factor is about : the relative error grows from to about — exactly digits lost.
📐The quadratic formula, done right
For the roots are about and . The textbook formula for the small root is . In double precision agrees with in its first 16 digits — the subtraction leaves only rounding noise, and the computed root is : 25% wrong.
The stable route: compute the large root without cancellation, , then use the product of the roots, :
Now the small root is , correct to full precision. Same equation, same arithmetic, a different order of operations.
Derivation
Where 25% goes, and why is safe
The textbook route. Here , , , and . Near (between and ) doubles are spaced apart, so the square root is stored as , the nearest double. Then
The subtraction itself was exact; the answer is simply one rounding gap of the square root, halved. Every digit of the true root, , was already lost when was rounded.
The stable route. If and are the roots, . Comparing the constant terms: . The quadratic formula's two roots are ; choosing the sign that makes and the square root add (both positive here) gives the large root with no cancellation:
The small root then follows from the product, — one division, correctly rounded.
Floating-point arithmetic is considered an esoteric subject by many people.
Summation is not as simple as it looks. Adding numbers one at a time gives an error bound proportional to times the size of the running sums. Once the total is large, each small new term is rounded to the total's coarse spacing and partly lost. Adding 0.1 a million times in single precision gives 100958.34 instead of 100000.
Three fixes, in increasing order of effort:
- Pairwise summation — add in a balanced tree. The error grows like instead of . NumPy's
np.sumdoes this by default. - Kahan compensated summation (William Kahan, 1965) — keep a second variable that captures the low-order bits each addition drops, and feed them back into the next addition. The error becomes independent of to first order.
- Exactly rounded summation — Python's
math.fsumtracks partial sums exactly and rounds once at the end.
One step of Kahan summation. In exact arithmetic c is always 0; in floating point (t − s) − y recovers exactly what the addition s + y rounded away.
⚠Relative and absolute error
Absolute error has units; relative error does not and counts significant digits. A position error of 1 m is excellent for a GNSS receiver on Earth's surface and useless for a docking spacecraft. Always ask which one your requirement is written in — and remember that relative error is undefined when the true value is zero.
💡Where you will use this
GNSS Navigation forms differences of nearly equal pseudoranges and carrier phases all the time; double-differencing is cancellation managed on purpose. Orbital Mechanics: the energy is a difference of two large numbers, and its error controls orbit-determination accuracy. Structures and CFD: residual norms are long sums, where pairwise or compensated summation decides whether a solver can converge below .
📚Further reading
- Nicholas Higham, Accuracy and Stability of Numerical Algorithms (2nd ed., SIAM, 2002), chapter 4, 'Summation' — every summation method in this lesson, with error bounds.
- William Kahan, 'Further remarks on reducing truncation errors', Communications of the ACM 8(1), 40 (1965) — the one-page note that introduced compensated summation.
- Michael Heath, Scientific Computing: An Introductory Survey (2nd ed., 2002; SIAM Classics 2018), chapter 1, 'Scientific Computing' — sources of error, cancellation and the quadratic formula.
↓But there is a problem
Rounding error comes from the machine, and we now know how to keep it small: avoid cancellation, sum carefully, prefer formulas that never subtract nearly equal numbers.
But most of the errors in a numerical method are not rounding errors at all. They are put there deliberately. To compute the machine replaces an infinite series with a finite one. To compute a derivative it replaces a limit with a small step.
In 1972 Hewlett-Packard put sine, cosine, logarithms and exponentials into a pocket calculator whose chip could do little more than add and shift. Next lesson: how — and how much error that costs.