Algorithm Search⌘K

Proofs: TwoSum (Knuth)

These are the human proofs of TwoSum (Knuth), at full length.

TwoSum returns the exact error of a rounded addition full

This proof shows: correctness.

Setting. Binary floating-point arithmetic with precision p≥3p \ge 3, minimal exponent emin⁡e_{\min}, subnormal numbers, and no overflow in any operation. Every operation is rounded to nearest, written RN\mathrm{RN}, with a tie rule that is symmetric (RN(−v)=−RN(v)\mathrm{RN}(-v) = -\mathrm{RN}(v); ties to even and ties away from zero both are). For a float x≠0x \ne 0 let exe_x be the integer with 2ex≤∣x∣<2ex+12^{e_x} \le |x| < 2^{e_x + 1} if xx is normal and ex=emin⁡e_x = e_{\min} if xx is subnormal; let e0=emin⁡e_0 = e_{\min}, and Ux=2ex−p+1U_x = 2^{e_x - p + 1}. Every float xx is an integer multiple of UxU_x. If e≥emin⁡e \ge e_{\min} and U=2e−p+1U = 2^{e - p + 1}, every number kUkU with kk an integer and ∣k∣≤2p|k| \le 2^p is a float.

We use three facts about RN\mathrm{RN}: it is monotone; RN(v)=v\mathrm{RN}(v) = v for a float vv; and ∣RN(v)−v∣≤∣f−v∣|\mathrm{RN}(v) - v| \le |f - v| for every float ff. We also use the theorem on Fast2Sum: if ex≥eye_x \ge e_y, then RN(x+y)−x\mathrm{RN}(x + y) - x is a float, and so is (x+y)−RN(x+y)(x + y) - \mathrm{RN}(x + y), with absolute value at most 12URN(x+y)\tfrac12 U_{\mathrm{RN}(x+y)}. By ordering the operands, it follows that the rounding error of a sum or difference of two floats is a float (fact E).

The algorithm. s=RN(a+b)s = \mathrm{RN}(a + b), a′=RN(s−b)a' = \mathrm{RN}(s - b), b′=RN(s−a′)b' = \mathrm{RN}(s - a'), δa=RN(a−a′)\delta_a = \mathrm{RN}(a - a'), δb=RN(b−b′)\delta_b = \mathrm{RN}(b - b'), t=RN(δa+δb)t = \mathrm{RN}(\delta_a + \delta_b).

Theorem. s+t=a+bs + t = a + b.

Reduction. It suffices to show that

(i) a−a′ is a float,(ii) s−a′ is a float,(iii) b−(s−a′) is a float.\text{(i) } a - a' \text{ is a float},\qquad \text{(ii) } s - a' \text{ is a float},\qquad \text{(iii) } b - (s - a') \text{ is a float}.

Indeed, (ii) gives b′=s−a′b' = s - a', then (i) and (iii) give δa=a−a′\delta_a = a - a' and δb=b−b′\delta_b = b - b', and δa+δb=a+b−(a′+b′)=a+b−s\delta_a + \delta_b = a + b - (a' + b') = a + b - s, which is a float by fact E, so t=a+b−st = a + b - s.

Moreover (iii) follows from (ii): if b′=s−a′b' = s - a', then b−b′=a′−(s−b)=RN(s−b)−(s−b)b - b' = a' - (s - b) = \mathrm{RN}(s - b) - (s - b), which is minus the rounding error of the difference s−bs - b of two floats, a float by fact E. So only (i) and (ii) remain. Negating aa and bb negates every intermediate value (symmetry of RN\mathrm{RN}), so we may assume a+b≥0a + b \ge 0 when convenient; the arguments below do not depend on signs except where stated.

Case 1: the sum is exact, s=a+bs = a + b. Then s−b=as - b = a, so a′=aa' = a and a−a′=0a - a' = 0, and s−a′=bs - a' = b. Both are floats.

Case 2: eb≥eae_b \ge e_a. By the Fast2Sum theorem for (b,a)(b, a), s−bs - b is a float, so a′=s−ba' = s - b, and a−a′=a+b−sa - a' = a + b - s is a float. Also s−a′=bs - a' = b. So (i) and (ii) hold.

Case 3: ea>ebe_a > e_b and s≠a+bs \ne a + b. Then a≠0a \ne 0, b≠0b \ne 0, and ea>eb≥emin⁡e_a > e_b \ge e_{\min}, so aa is normal and ∣b∣<2eb+1≤2ea≤∣a∣|b| < 2^{e_b + 1} \le 2^{e_a} \le |a|. Let r=(a+b)−s≠0r = (a + b) - s \ne 0. By the Fast2Sum theorem for (a,b)(a, b), z=s−az = s - a is a float, r=b−zr = b - z is a float, and ∣r∣≤12Us|r| \le \tfrac12 U_s. Since ss is inexact, it is normal (sums in the subnormal range are exact).

Bounds. ∣a+b∣≤∣a∣+∣b∣<2∣a∣|a + b| \le |a| + |b| < 2|a|, and 2∣a∣2|a| is a float, so by monotonicity ∣s∣≤2∣a∣<2ea+2|s| \le 2|a| < 2^{e_a + 2}; hence es≤ea+1e_s \le e_a + 1, Us≤2UaU_s \le 2U_a and ∣r∣≤Ua|r| \le U_a.

We have s−b=a−rs - b = a - r and a′=RN(a−r)a' = \mathrm{RN}(a - r). Since aa is a float, ∣a′−(a−r)∣≤∣r∣|a' - (a - r)| \le |r|, so ∣a′−a∣≤2∣r∣≤2Ua=2ea−p+2≤2ea−1|a' - a| \le 2|r| \le 2U_a = 2^{e_a - p + 2} \le 2^{e_a - 1}, using p≥3p \ge 3. Hence ∣a′∣≥2ea−1|a'| \ge 2^{e_a - 1} and a′a' has the sign of aa; so ea′≥ea−1e_{a'} \ge e_a - 1 and a′a' is a multiple of V=Ua/2=2(ea−1)−p+1V = U_a/2 = 2^{(e_a - 1) - p + 1}, as is aa. Note ea−1≥emin⁡e_a - 1 \ge e_{\min}.

(i). a−a′a - a' is a multiple kVkV of VV with ∣kV∣≤2Ua=4V|kV| \le 2U_a = 4V, so ∣k∣≤4≤2p|k| \le 4 \le 2^p, and a−a′a - a' is a float.

(ii). If a′=aa' = a, then s−a′=zs - a' = z, a float. Suppose a′≠aa' \ne a.

Case 3a: eb≤ea−2e_b \le e_a - 2. Write s−a′=b−ρs - a' = b - \rho with ρ=a′−(s−b)\rho = a' - (s - b), so ∣ρ∣≤∣r∣≤Ua|\rho| \le |r| \le U_a. Then ∣s−a′∣≤∣b∣+Ua<2ea−1+2ea−p+1≤2ea|s - a'| \le |b| + U_a < 2^{e_a - 1} + 2^{e_a - p + 1} \le 2^{e_a}, since p≥2p \ge 2. Next, a′≠aa' \ne a means that a−ra - r is at least as close to a float other than aa as to aa; the floats adjacent to aa are at distance at least VV from aa, so ∣r∣≥V/2|r| \ge V/2, and with ∣r∣≤12Us|r| \le \tfrac12 U_s this gives Us≥VU_s \ge V. Thus ss and a′a' are both multiples of VV, and s−a′=kVs - a' = kV with ∣kV∣<2ea=2pV|kV| < 2^{e_a} = 2^p V, so ∣k∣<2p|k| < 2^p and s−a′s - a' is a float.

Case 3b: eb=ea−1e_b = e_a - 1. Then Ub=VU_b = V, and aa, bb, a+ba + b are multiples of VV. Since s≠a+bs \ne a + b, Us>VU_s > V, so Us∈{Ua,2Ua}U_s \in \{U_a, 2U_a\} and ss is a multiple of Ua=2VU_a = 2V. Hence r=a+b−sr = a + b - s is a nonzero multiple of VV with ∣r∣≤12Us≤Ua=2V|r| \le \tfrac12 U_s \le U_a = 2V: r=±Vr = \pm V or r=±2Vr = \pm 2V.

  • If a−ra - r is a float, then a′=a−ra' = a - r and s−a′=s−a+r=z+r=bs - a' = s - a + r = z + r = b, a float.
  • Otherwise a−ra - r lies strictly between two consecutive floats. If ∣r∣=2V=Ua|r| = 2V = U_a, then a−r=a∓Uaa - r = a \mp U_a, which is a float: floats in the binade of aa are spaced UaU_a, the number 2ea+12^{e_a + 1} is a float, and below 2ea2^{e_a} the floats are spaced VV, so a step of UaU_a from a float of magnitude at least 2ea2^{e_a} lands on a float. So ∣r∣=V|r| = V, a−r=a∓Va - r = a \mp V is the midpoint between aa and its neighbour a∓Uaa \mp U_a in the binade, and a′≠aa' \ne a forces a′=a−2ra' = a - 2r. Then s−a′=z+2r=b+rs - a' = z + 2r = b + r. This is a multiple of VV with ∣b+r∣≤∣b∣+V<2ea+V|b + r| \le |b| + V < 2^{e_a} + V; since ∣b∣<2eb+1=2ea|b| < 2^{e_b + 1} = 2^{e_a} is a multiple of VV, ∣b∣≤2ea−V|b| \le 2^{e_a} - V, so ∣b+r∣≤2ea=2pV|b + r| \le 2^{e_a} = 2^p V, and b+rb + r is a float.

In every case (i) and (ii) hold, which proves the theorem. □\square

The bound on tt. t=a+b−st = a + b - s is the rounding error of ss, so ∣t∣≤12ulp(s)|t| \le \tfrac12\mathrm{ulp}(s) by the Fast2Sum theorem, and 12ulp(s)≤u∣s∣\tfrac12\mathrm{ulp}(s) \le u|s| for normal ss (if ss is subnormal, t=0t = 0).

Remarks. The proof uses round to nearest in fact E and in ∣a′−(a−r)∣≤∣r∣|a' - (a - r)| \le |r|. With directed rounding the rounding error of an addition need not be a float, and the theorem fails. The hypothesis p≥3p \ge 3 is used only to keep a′a' in the binade of aa or the one below; an exhaustive machine check of all pairs in a small format with p=2p = 2 found no counterexample, but this is not a proof for p=2p = 2.

Back to TwoSum (Knuth)