TwoSum returns the exact error of a rounded addition full
This proof shows: correctness.
Setting. Binary floating-point arithmetic with precision p≥3, minimal exponent emin, subnormal numbers, and no overflow in any operation. Every operation is rounded to nearest, written RN, with a tie rule that is symmetric (RN(−v)=−RN(v); ties to even and ties away from zero both are). For a float x=0 let ex be the integer with 2ex≤∣x∣<2ex+1 if x is normal and ex=emin if x is subnormal; let e0=emin, and Ux=2ex−p+1. Every float x is an integer multiple of Ux. If e≥emin and U=2e−p+1, every number kU with k an integer and ∣k∣≤2p is a float.
We use three facts about RN: it is monotone; RN(v)=v for a float v; and ∣RN(v)−v∣≤∣f−v∣ for every float f. We also use the theorem on Fast2Sum: if ex≥ey, then RN(x+y)−x is a float, and so is (x+y)−RN(x+y), with absolute value at most 21URN(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), a′=RN(s−b), b′=RN(s−a′), δa=RN(a−a′), δb=RN(b−b′), t=RN(δa+δb).
Theorem. s+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. Indeed, (ii) gives b′=s−a′, then (i) and (iii) give δa=a−a′ and δb=b−b′, and δa+δb=a+b−(a′+b′)=a+b−s, which is a float by fact E, so t=a+b−s.
Moreover (iii) follows from (ii): if b′=s−a′, then b−b′=a′−(s−b)=RN(s−b)−(s−b), which is minus the rounding error of the difference s−b of two floats, a float by fact E. So only (i) and (ii) remain. Negating a and b negates every intermediate value (symmetry of RN), so we may assume a+b≥0 when convenient; the arguments below do not depend on signs except where stated.
Case 1: the sum is exact, s=a+b. Then s−b=a, so a′=a and a−a′=0, and s−a′=b. Both are floats.
Case 2: eb≥ea. By the Fast2Sum theorem for (b,a), s−b is a float, so a′=s−b, and a−a′=a+b−s is a float. Also s−a′=b. So (i) and (ii) hold.
Case 3: ea>eb and s=a+b. Then a=0, b=0, and ea>eb≥emin, so a is normal and ∣b∣<2eb+1≤2ea≤∣a∣. Let r=(a+b)−s=0. By the Fast2Sum theorem for (a,b), z=s−a is a float, r=b−z is a float, and ∣r∣≤21Us. Since s is inexact, it is normal (sums in the subnormal range are exact).
Bounds. ∣a+b∣≤∣a∣+∣b∣<2∣a∣, and 2∣a∣ is a float, so by monotonicity ∣s∣≤2∣a∣<2ea+2; hence es≤ea+1, Us≤2Ua and ∣r∣≤Ua.
We have s−b=a−r and a′=RN(a−r). Since a is a float, ∣a′−(a−r)∣≤∣r∣, so ∣a′−a∣≤2∣r∣≤2Ua=2ea−p+2≤2ea−1, using p≥3. Hence ∣a′∣≥2ea−1 and a′ has the sign of a; so ea′≥ea−1 and a′ is a multiple of V=Ua/2=2(ea−1)−p+1, as is a. Note ea−1≥emin.
(i). a−a′ is a multiple kV of V with ∣kV∣≤2Ua=4V, so ∣k∣≤4≤2p, and a−a′ is a float.
(ii). If a′=a, then s−a′=z, a float. Suppose a′=a.
Case 3a: eb≤ea−2. Write s−a′=b−ρ with ρ=a′−(s−b), so ∣ρ∣≤∣r∣≤Ua. Then ∣s−a′∣≤∣b∣+Ua<2ea−1+2ea−p+1≤2ea, since p≥2. Next, a′=a means that a−r is at least as close to a float other than a as to a; the floats adjacent to a are at distance at least V from a, so ∣r∣≥V/2, and with ∣r∣≤21Us this gives Us≥V. Thus s and a′ are both multiples of V, and s−a′=kV with ∣kV∣<2ea=2pV, so ∣k∣<2p and s−a′ is a float.
Case 3b: eb=ea−1. Then Ub=V, and a, b, a+b are multiples of V. Since s=a+b, Us>V, so Us∈{Ua,2Ua} and s is a multiple of Ua=2V. Hence r=a+b−s is a nonzero multiple of V with ∣r∣≤21Us≤Ua=2V: r=±V or r=±2V.
- If a−r is a float, then a′=a−r and s−a′=s−a+r=z+r=b, a float.
- Otherwise a−r lies strictly between two consecutive floats. If ∣r∣=2V=Ua, then a−r=a∓Ua, which is a float: floats in the binade of a are spaced Ua, the number 2ea+1 is a float, and below 2ea the floats are spaced V, so a step of Ua from a float of magnitude at least 2ea lands on a float. So ∣r∣=V, a−r=a∓V is the midpoint between a and its neighbour a∓Ua in the binade, and a′=a forces a′=a−2r. Then s−a′=z+2r=b+r. This is a multiple of V with ∣b+r∣≤∣b∣+V<2ea+V; since ∣b∣<2eb+1=2ea is a multiple of V, ∣b∣≤2ea−V, so ∣b+r∣≤2ea=2pV, and b+r is a float.
In every case (i) and (ii) hold, which proves the theorem. □
The bound on t. t=a+b−s is the rounding error of s, so ∣t∣≤21ulp(s) by the Fast2Sum theorem, and 21ulp(s)≤u∣s∣ for normal s (if s is subnormal, t=0).
Remarks. The proof uses round to nearest in fact E and in ∣a′−(a−r)∣≤∣r∣. With directed rounding the rounding error of an addition need not be a float, and the theorem fails. The hypothesis p≥3 is used only to keep a′ in the binade of a or the one below; an exhaustive machine check of all pairs in a small format with p=2 found no counterexample, but this is not a proof for p=2.