Catastrophic Cancellation
Subtracting two nearly equal floating-point numbers annihilates leading digits and amplifies the relative error already present in the inputs.
Losing significance in subtraction
When two nearly equal numbers are subtracted, their agreeing leading digits cancel and the result is dominated by the trailing, less accurate digits. The absolute error stays the same, but the relative error explodes because the result is small. This is catastrophic cancellation, and it is one of the most common sources of inaccuracy in numerical code.
A classic example is the quadratic formula. For b much larger than 4ac, one root uses -b + sqrt(b^2 - 4ac), subtracting two close numbers. The fix is to compute the well-conditioned root first, then obtain the other from the identity x1 x2 = c/a.
A worked example
Computing (1 - cos x)/x^2 for small x subtracts cos x, which is near 1, from 1. For x = 1e-8 the naive formula returns nonsense, while the mathematically equivalent form using a half-angle identity, or a Taylor series, stays accurate.
import math
x = 1e-8
naive = (1 - math.cos(x)) / x**2 # ~0.0, wildly wrong
stable = (math.sin(x/2)/(x/2))**2 / 2 # ~0.5, correct
print(naive, stable)
Recognizing and avoiding it
Cancellation is dangerous only when the inputs already carry rounding error. Reformulate expressions to avoid subtracting near-equal quantities: use trigonometric or algebraic identities, rationalize numerators, or sum series terms of like sign. Compensated summation (Kahan) recovers digits lost when accumulating many terms.
Careful reformulation matters in physics kernels: computing small energy differences or near-cancelling force terms, as in the field solvers used for the breeder Hyperion, requires expressions arranged so that the dominant terms never subtract away the quantity of interest.