Grow-Expansion keeps an exact nonoverlapping expansion, and the final pass rounds it correctly full
This proof shows: correctness.
Setting. Binary floating point with precision p≥2, gradual underflow, and round to nearest. Ties go to even for the final result, and any tie rule is allowed elsewhere. No operation overflows. For a nonzero float x, ϵ(x) is the largest power of two dividing x, and ϵ(0)=∞. An expansion is a list e1,…,em of nonzero floats with ∣ek∣<ϵ(ek+1). TwoSum(a,b) returns s=fl(a+b) and t=a+b−s exactly. Fast2Sum(a,b) returns the same pair when ∣a∣≥∣b∣. Both facts are standard (Knuth Vol. 2 Section 4.2.2, Dekker 1971) and hold with underflow.
Lemma A (TwoSum error). Let (s,t)=TwoSum(a,b). Then ∣t∣≤min(∣a∣,∣b∣), and if t=0 then s=0 and ∣t∣<ϵ(s).
Proof. a is a float at distance ∣b∣ from a+b. The nearest float s is no farther, so ∣t∣≤∣b∣, and by symmetry ∣t∣≤∣a∣. If t=0, then a+b is not a float, so it lies strictly between two consecutive floats f1<f2. Their distance d=f2−f1 is a power of two, and both are multiples of d. With gradual underflow, every multiple of 2emin−p+1 near 0 is a float, so neither f1 nor f2 is 0 unless a+b is already a float. Hence s∈{f1,f2} is nonzero, ϵ(s)≥d, and ∣t∣≤d/2<ϵ(s). □
Lemma B (grid). If a and b are multiples of a power of two g, then so are s=fl(a+b) and t=a+b−s.
Proof. If a+b is a float, then s=a+b and t=0. Otherwise a+b lies strictly between consecutive floats f1,f2 at distance d, which are multiples of d. So d does not divide a+b, but g does, and therefore d>g. As both are powers of two, g divides d, so g divides s∈{f1,f2} and t. □
Lemma C (domination). In an expansion, ∑l<k∣el∣<ϵ(ek) for every k. In particular ∣ek∣>∑l<k∣el∣, every partial sum ek+⋯+em is nonzero, and a nonzero sum ∑l<kel has the sign of its largest term.
Proof. ∣el∣<ϵ(el+1) means that all binary digits of ∣el∣ are below the lowest digit of el+1. So the digit sets of ∣e1∣,…,∣ek−1∣ are pairwise disjoint and all lie below position log2ϵ(ek). A sum of numbers with disjoint digit sets has no carries, so it is below ϵ(ek). The other claims follow, because ∣ek∣≥ϵ(ek). □
Theorem 1 (Grow-Expansion). Let e1,…,em be an expansion and x a float. Set Q0=x and (Qk,hk)=TwoSum(Qk−1,ek) for k=1,…,m. Then the list of nonzero hk in order, followed by Qm if it is nonzero, is an expansion with value x+∑kek.
Proof. Value. Qk+hk=Qk−1+ek for each k. Summing these equations gives Qm+∑khk=x+∑kek.
Nonoverlap. Let gk=min(ϵ(Qk−1),ϵ(ek)), a power of two. By Lemma B, Qk and hk are multiples of gk. We show by induction on k that for all i<k,
(∗)∣hi∣<ϵ(ek)and∣hi∣<ϵ(Qk−1). The first part holds for every k>i, by Lemmas A and C: ∣hi∣≤∣ei∣≤∑l<k∣el∣<ϵ(ek). For the second part with k=i+1, Lemma A gives ∣hi∣<ϵ(Qi), or hi=0. For k>i+1, the induction hypothesis for k−1 gives ∣hi∣<gk−1, and gk−1 divides Qk−1, so ϵ(Qk−1)≥gk−1>∣hi∣.
From (∗), ∣hi∣<gk≤ϵ(hk) whenever i<k and hk=0. The same argument with k=m+1, using gm+1:=ϵ(Qm), gives ∣hi∣<ϵ(Qm) for i<m. Lemma A gives ∣hm∣<ϵ(Qm). So every pair of consecutive components of the output, after the zeros are removed, satisfies the expansion condition. □
Theorem 2 (rounding). Let e1,…,em be a nonempty expansion with value S. Lines L8 to L11 return RN(S), the nearest float with ties to even.
Proof. Write σj=ej+⋯+em. Every el with l≥j is a multiple of ϵ(ej), because ϵ(el) increases with l.
Exact phase. Suppose that before ej is processed, s=σj+1. This is true at the start. σj+1 is a nonzero multiple of ϵ(ej+1) (Lemma C), so ∣s∣≥ϵ(ej+1)>∣ej∣, and Fast2Sum is exact. If its error is 0, then s=σj and the invariant continues. If the loop runs to the end, then s=σ1=S is a float, L10 does nothing, and the result is S.
First inexact step. Otherwise the loop breaks at some j with s=RN(σj) and t=σj−s=0. Let R=∑l<jel, so S=s+t+R. By Lemma B with g=ϵ(ej), t is a nonzero multiple of ϵ(ej). By Lemma C, ∣R∣<ϵ(ej)≤∣t∣, and if j>1 then R=0 has the sign of ej−1. Let D be the distance from s to the next float in the direction of t. It is a power of two, and ∣t∣≤D/2 because s is nearest to s+t. D/2≥∣t∣≥ϵ(ej) is a power of two, so it is a multiple of ϵ(ej). Line L10 computes 2t exactly. Also, fl(s+2t)−s is computed exactly, since it is 0 or the float ±D, the difference of two adjacent floats.
- Case ∣t∣<D/2. Then ∣t∣≤D/2−ϵ(ej), so t+R has the sign of t and ∣t+R∣<D/2. S lies strictly between s and the midpoint in that direction, so RN(S)=s. In L10, s+2t lies strictly between s and its neighbour, so fl(s+2t)−s∈{0,±D} differs from 2t, and s is kept.
- Case ∣t∣=D/2, j=1. Then S=s+t is a midpoint, and s=RN(σ1) was chosen by ties to even. L10 does not fire.
- Case ∣t∣=D/2, j>1, R with the sign of t. Then D/2<∣t+R∣<D/2+ϵ(ej)≤D. S is past the midpoint, so RN(S)=s+2t, the neighbour. In L10, s+2t is a float, so fl(s+2t)−s=2t, and the step is taken.
- Case ∣t∣=D/2, j>1, R with the opposite sign. Then 0<∣t+R∣<D/2, so RN(S)=s. The sign test in L10 fails, and s is kept.
In every case the result is RN(S). □
Corollary. SHEWCHUK-SUM returns RN(x1+⋯+xn). Proof. The empty list is an expansion with value 0. By Theorem 1 and induction on i, after step i the list e is an expansion with value x1+⋯+xi. If it is empty, the sum is 0 and the result is 0. Otherwise Theorem 2 applies. ■