Key idea of this section
The relative condition number of subtraction at (a, b) is (|a| + |b|)/|a − b|. Subtracting nearly equal numbers is itself exact (Sterbenz), but it magnifies the errors already present in the operands: catastrophic cancellation. Forward error ≲ condition number × backward error.
Conditioning
Let f be a differentiable function and x ≠ 0 with f(x) ≠ 0. If x is perturbed to x(1 + δ), then f changes by approximately f′(x) x δ, a relative change of κ(x) δ where
κ(x) = |x f′(x) / f(x)|
is the relative condition number. It is a property of the problem, not of any algorithm. For f(x) = √x, κ = 1/2 everywhere: square roots are perfectly conditioned. For f(x) = ln x, κ = 1/|ln x|, which is large near x = 1. For the function of two variables (a, b) ↦ a − b, if a and b carry relative errors at most η then
|(â − b̂) − (a − b)| / |a − b| ≤ η (|a| + |b|) / |a − b|
since |â − a| + |b̂ − b| ≤ η(|a| + |b|). The factor (|a| + |b|)/|a − b| is unbounded as a → b: this is cancellation.
Theorem 11.1 (Sterbenz lemma, 1974). If x and y are floating-point numbers with y/2 ≤ x ≤ 2y, then x − y is exactly representable, so x ⊖ y = x − y.
Proof. The hypothesis forces y ≥ 0 and x ≥ 0, and if y = 0 then x = 0; so assume y > 0. The hypothesis is symmetric in x and y (it is equivalent to x/2 ≤ y ≤ 2x), so we may also assume y ≤ x ≤ 2y. Let 2ᵉ ≤ y < 2ᵉ⁺¹ in the normal range. Both x and y are integer multiples of the quantum 2e−p+1, since x ≥ y lies in the same or a higher binade, where the spacing is a multiple of that of y's binade. Hence x − y is an integer multiple of 2e−p+1, and 0 ≤ x − y ≤ y < 2ᵉ⁺¹, so x − y = k · 2e−p+1 with 0 ≤ k < 2ᵖ, which is representable (as a normal or, if small, subnormal number). If y is subnormal the same argument works with the fixed subnormal quantum. ∎
The lesson of the lemma is subtle: the subtraction introduces no error at all. Cancellation is catastrophic when the operands are themselves the result of earlier rounding, because the subtraction strips away the leading digits that agree and exposes the inaccurate trailing ones. Cancellation of exact data is benign.
Three case studies
Example 11.2 — (1 − cos x)/x². The true value tends to 1/2 as x → 0. In binary64 at x = 10⁻⁸, cos x = 1 − 5 × 10⁻¹⁷ + ⋯, which is closer to 1 than to the next binary64 number below 1, namely 1 − 2⁻⁵³ ≈ 1 − 1.11 × 10⁻¹⁶. So the computed cosine is exactly 1, and the formula returns 0, with relative error 100 percent. The identity 1 − cos x = 2 sin²(x/2) removes the subtraction; evaluating 2 sin²(x/2)/x² in binary64 gives 0.5000000000000001, correct to rounding.
Example 11.3 — the quadratic formula. The roots of x² − 10⁸x + 1 = 0 are x = (10⁸ ± √(10¹⁶ − 4))/2, approximately 10⁸ and 10⁻⁸ (more precisely, the small root is 10⁻⁸ + 10⁻²⁴ + ⋯). In binary64, √(10¹⁶ − 4) ≈ 10⁸ − 2 × 10⁻⁸ is rounded to the nearest number in a binade where the spacing is 2⁻²⁶ ≈ 1.49 × 10⁻⁸, namely 10⁸ − 2⁻²⁶. The naive formula then returns 2⁻²⁶/2 = 2⁻²⁷ ≈ 7.4506 × 10⁻⁹ for the small root, which is wrong by about 25 percent. The stable method, writing the equation as x² − bx + c = 0 with b > 0, computes the large root x₁ = (b + √(b² − 4c))/2, where no cancellation occurs, and then the small root from Vieta's relation x₁x₂ = c, giving x₂ = 1/x₁ = 1.0000000000000000 × 10⁻⁸, correct to full precision.
Example 11.4 — √(x + 1) − √x for large x. At x = 10¹⁵ the naive difference in binary64 is 1.862645149230957 × 10⁻⁸, while the algebraically equivalent 1/(√(x + 1) + √x) gives 1.5811388300841893 × 10⁻⁸, the correctly rounded value. The naive result is off by about 18 percent: the two square roots agree in their first eight digits, and the subtraction leaves only the rounding errors of the square roots.
Summation
Theorem 11.5. Let Ŝ be the result of summing x₁, …, xₙ left to right in floating-point arithmetic with unit roundoff u, and let γₖ = ku/(1 − ku) for ku < 1. Then
|Ŝ − Σ xᵢ| ≤ γₙ₋₁ Σ |xᵢ|.
Proof. By the standard model, the computed partial sums satisfy Ŝₖ = (Ŝₖ₋₁ + xₖ)(1 + δₖ) with |δₖ| ≤ u. Unrolling, Ŝ = Σ xᵢ ∏ (k = max(i, 2) to n) (1 + δₖ), each product having at most n − 1 factors. A product of j factors (1 + δ) with |δ| ≤ u lies within γⱼ of 1 (an elementary induction), and the bound follows. ∎
The bound, roughly (n − 1)u Σ|xᵢ|, is a worst case; with random rounding errors the typical error grows like √n u. The ordering matters: summing positive terms in increasing order reduces the error. William Kahan's compensated summation (1965) carries a running correction term c:
s = 0, c = 0 for each x: y = x − c; t = s + y; c = (t − s) − y; s = t
The quantity (t − s) − y recovers, exactly in many cases, the low-order part of y that was lost in forming t. The error bound becomes (2u + O(nu²)) Σ|xᵢ|, essentially independent of n. Adding 0.1 ten times in binary64 gives 0.9999999999999999 by naive summation and exactly 1 with Kahan's algorithm.
Backward stability
James Wilkinson's backward error analysis, developed in the 1950s and 1960s for matrix computations, changed the way numerical analysts think. Instead of asking how far the computed answer is from the true one, ask for which nearby data the computed answer is exact. An algorithm is backward stable if its output is the exact result for data perturbed by a relative amount of order u. Recursive summation is backward stable: the result above is the exact sum of the perturbed numbers xᵢ(1 + θᵢ) with |θᵢ| ≤ γₙ₋₁. Combining backward error with conditioning gives the rule of thumb
relative forward error ≲ κ × relative backward error
so a backward-stable algorithm applied to a well-conditioned problem gives an accurate answer, and no algorithm can be expected to do better than κu on an ill-conditioned one. The naive quadratic formula in Example 11.3 is not backward stable for the small root; the Vieta-based method is. The fused multiply-add, which computes xy + z with a single rounding, is a further tool: it allows the exact computation of the rounding error of a product, since xy − fl(xy) is a floating-point number that FMA returns exactly.
Exercise
- (a)Compute the relative condition number of f(x) = eˣ and of f(x) = x − 1, and say where each is ill-conditioned.
- (b)Give an expression for ln(1 + x) − x, for small |x|, that avoids cancellation, to within a relative error O(x).
- (c)For x = 1.5 and y = 1 in binary64, decide whether the Sterbenz lemma applies and compute x ⊖ y.
Show solution
Answers: (a) For eˣ, κ = |x · eˣ/eˣ| = |x|, ill-conditioned only for large |x|. For x − 1, κ = |x/(x − 1)|, ill-conditioned near x = 1. (b) From the Taylor series ln(1 + x) − x = −x²/2 + x³/3 − ⋯, the truncation −x²/2 + x³/3 has relative error O(x²) and −x²/2 alone has relative error O(x); in practice one uses the library function log1p(x) and subtracts x only when x is not small. (c) y/2 = 0.5 ≤ 1.5 ≤ 2 = 2y, so the lemma applies and x ⊖ y = 0.5 exactly.