What is Catastrophic cancellation?
Loss of significant digits when subtracting two nearly equal numbers, as their leading digits cancel and the relative weight of existing rounding error grows. It is avoided by rearranging formulas.
Field: Computer Arithmetic
Japanese name: 桁落ち
The subtraction is not the culprit
Surprisingly, subtracting two nearby floating-point numbers is usually exact: if x and y are within a factor of two of each other, x minus y is exactly representable and the hardware returns it without rounding (a result known as Sterbenz's lemma). The damage comes from errors already present in the operands, from earlier rounding, measurement or conversion. When the leading digits cancel, those small absolute errors remain while the result itself becomes tiny, so the relative error is magnified. Cancellation exposes old errors rather than creating new ones.
How large the amplification is
If a and b each carry a relative error of about e, the error in a minus b is roughly e multiplied by (|a| + |b|) / |a - b|. Subtracting numbers that agree in their first six significant digits therefore amplifies the relative error by about a million, which consumes six decimal digits of a double's roughly sixteen. If the operands agree to all their digits, the result can be pure noise or exactly zero. This ratio is the condition number of subtraction, and it explains why the effect depends on how close the inputs are, not on the format alone.
Standard rewrites
The cure is algebra that avoids subtracting close values. Rationalizing turns into . In the quadratic formula, when b² is much larger than 4ac, one root suffers cancellation; computing the other root first and obtaining the troublesome one from the product of roots, c / a divided by the first root, avoids it. For small x, 1 − cos x becomes 2 sin²(x / 2), and math libraries provide expm1(x) and log1p(x) to compute exp(x) − 1 and log(1 + x) accurately near zero.
Cancellation vs. absorption
Cancellation and absorption are the two classic ways floating point loses information, and they are easily confused. Cancellation happens when subtracting nearly equal numbers and leaves too few meaningful digits in the result. Absorption happens when adding numbers of very different magnitude: the smaller operand's low digits fall off the end of the significand, as when 1.0e16 + 1 evaluates to exactly 1.0e16 in double precision. Moving to a wider format postpones both problems but does not remove them; reformulating the computation does.
Try it: questions about Catastrophic cancellation
- ACompute
- BCompute
- CUse double precision for x and subtract with the same formula
Show answer and explanation
Answer: A. Compute
and √x are nearly equal, so subtracting them directly cancels the leading digits. Multiplying numerator and denominator by gives, eliminating the subtraction of close values. equals 1 and is simply wrong. Computing in double precision from the start helps, but the same problem returns for larger x, so it is not a fundamental fix.
FAQ
Why does subtracting nearly equal floating-point numbers lose precision?
The matching leading digits cancel, leaving a small result made mostly of the low-order digits, which are the ones already contaminated by rounding error. The absolute error stays the same while the value shrinks, so the relative error grows sharply.
Does switching from float to double fix catastrophic cancellation?
It helps by giving more digits to lose, but the same cancellation reappears when the operands are closer still. Rewriting the formula to avoid the subtraction is the real fix.
How do you avoid cancellation in the quadratic formula?
Compute the root in which -b and the square root term have the same sign, so they add rather than cancel, then get the other root from the relation that the product of the roots equals c / a.
Books on Computer Arithmetic Ad
As an Amazon Associate I earn from qualifying purchases.
Semiconductor, Electronics and Computing Calculators
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.
Back to the glossary