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.

Articles by others on the same topic (NaN)

Catastrophic cancellation is a numerical phenomenon that occurs when subtracting two nearly equal numbers, resulting in a significant loss of precision in the result. This can happen in floating-point arithmetic, where the limited number of significant digits affects the accuracy of computations. When two close numbers are subtracted, their leading digits can cancel out, and only the less significant digits remain, which may be subject to rounding errors.