Catastrophic cancellation 2026-10-14
Catastrophic cancellation occurs when subtraction of nearly equal approximations removes their matching leading digits and exposes earlier roundoff error. The subtraction can itself be exact on the stored operands: the information may already have been lost while obtaining them. Goldberg distinguishes this from cancellation of accurately known quantities, which can be harmless.[ref]
For , consider the single real-valued function
The rationalization of a square-root difference proves equality in exact arithmetic. The two evaluations use the same input , but the direct version first forms a value near one and then subtracts one. The rearranged version keeps the small input in its numerator and divides by a denominator near two.
Figure 1.
Direct and rationalized evaluation of a small square-root difference against a high-precision reference for the same binary64 inputs
. Details.
For binary64 x = 1e-16, the rounded sum 1.0 + x is one, so sqrt(1.0 + x) - 1.0 is zero. The rationalized evaluation returns approximately . A 100-digit Decimal calculation using the exact stored input gives relative errors of one and about respectively. At x = 1e-12, the direct result is about , with relative error ; the rearranged result is about , with relative error .
The plot uses Decimal.from_float(x) to preserve the binary input exactly, computes at 100 decimal digits, and verifies the reference at 160 digits. Exact conversion of a stored binary value is documented by Python.[ref] Each error is , with the computed result converted exactly to Decimal before comparison. Hence the curves measure evaluation error for one common input, rather than a mixture of input conversion error and evaluation error. The plotted interval is far above the subnormal number range.
The original problem is well conditioned with respect to relative changes in . Using the derivative and rationalization of a square-root difference, its relative condition number is
It tends to one as tends to zero. The direct evaluation instead introduces the intermediate difference with ; an error of order unit roundoff in is large relative to . This is a failure of the chosen algorithm's accuracy despite a small condition number for the same real-valued function. The rationalized evaluation avoids that intermediate catastrophic cancellation, retaining relative accuracy near the unit roundoff scale throughout the plotted interval. Recognizing this structure can improve a calculation far more than merely printing extra digits.
Use rationalization of a square-root difference to avoid subtracting two large, nearly equal quantities:
Since and the square root is continuous at , the denominator tends to . The reciprocal limit law established above is applicable because this limit is nonzero. Hence
The limit is one half; rationalization exposes the finite difference hidden by cancellation.
Multiplying numerator and denominator of a fraction by the same nonzero expression can remove a square root from the denominator. For instance,
when and both denominators are nonzero. This uses the difference-of-squares identity . If , the original expression may still be defined even though the rationalized quotient is not, so that case must be handled separately. The related rationalization of a square-root difference applies the same multiplication to a numerator.