Algorithm Search⌘K

Proofs: Shewchuk expansion arithmetic

These are the human proofs of Shewchuk expansion arithmetic, at full length.

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≥2p \ge 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 xx, ϵ(x)\epsilon(x) is the largest power of two dividing xx, and ϵ(0)=∞\epsilon(0) = \infty. An expansion is a list e1,…,eme_1, \ldots, e_m of nonzero floats with ∣ek∣<ϵ(ek+1)|e_k| < \epsilon(e_{k+1}). TwoSum(a,b)(a, b) returns s=fl(a+b)s = \mathrm{fl}(a + b) and t=a+b−st = a + b - s exactly. Fast2Sum(a,b)(a, b) returns the same pair when ∣a∣≥∣b∣|a| \ge |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)(s, t) = \mathrm{TwoSum}(a, b). Then ∣t∣≤min⁡(∣a∣,∣b∣)|t| \le \min(|a|, |b|), and if t≠0t \ne 0 then s≠0s \ne 0 and ∣t∣<ϵ(s)|t| < \epsilon(s).

Proof. aa is a float at distance ∣b∣|b| from a+ba + b. The nearest float ss is no farther, so ∣t∣≤∣b∣|t| \le |b|, and by symmetry ∣t∣≤∣a∣|t| \le |a|. If t≠0t \ne 0, then a+ba + b is not a float, so it lies strictly between two consecutive floats f1<f2f_1 < f_2. Their distance d=f2−f1d = f_2 - f_1 is a power of two, and both are multiples of dd. With gradual underflow, every multiple of 2emin⁡−p+12^{e_{\min} - p + 1} near 0 is a float, so neither f1f_1 nor f2f_2 is 00 unless a+ba + b is already a float. Hence s∈{f1,f2}s \in \{f_1, f_2\} is nonzero, ϵ(s)≥d\epsilon(s) \ge d, and ∣t∣≤d/2<ϵ(s)|t| \le d/2 < \epsilon(s). □\square

Lemma B (grid). If aa and bb are multiples of a power of two gg, then so are s=fl(a+b)s = \mathrm{fl}(a + b) and t=a+b−st = a + b - s.

Proof. If a+ba + b is a float, then s=a+bs = a + b and t=0t = 0. Otherwise a+ba + b lies strictly between consecutive floats f1,f2f_1, f_2 at distance dd, which are multiples of dd. So dd does not divide a+ba + b, but gg does, and therefore d>gd > g. As both are powers of two, gg divides dd, so gg divides s∈{f1,f2}s \in \{f_1, f_2\} and tt. □\square

Lemma C (domination). In an expansion, ∑l<k∣el∣<ϵ(ek)\sum_{l < k} |e_l| < \epsilon(e_k) for every kk. In particular ∣ek∣>∑l<k∣el∣|e_k| > \sum_{l<k}|e_l|, every partial sum ek+⋯+eme_k + \cdots + e_m is nonzero, and a nonzero sum ∑l<kel\sum_{l<k} e_l has the sign of its largest term.

Proof. ∣el∣<ϵ(el+1)|e_l| < \epsilon(e_{l+1}) means that all binary digits of ∣el∣|e_l| are below the lowest digit of el+1e_{l+1}. So the digit sets of ∣e1∣,…,∣ek−1∣|e_1|, \ldots, |e_{k-1}| are pairwise disjoint and all lie below position log⁡2ϵ(ek)\log_2 \epsilon(e_k). A sum of numbers with disjoint digit sets has no carries, so it is below ϵ(ek)\epsilon(e_k). The other claims follow, because ∣ek∣≥ϵ(ek)|e_k| \ge \epsilon(e_k). □\square

Theorem 1 (Grow-Expansion). Let e1,…,eme_1, \ldots, e_m be an expansion and xx a float. Set Q0=xQ_0 = x and (Qk,hk)=TwoSum(Qk−1,ek)(Q_k, h_k) = \mathrm{TwoSum}(Q_{k-1}, e_k) for k=1,…,mk = 1, \ldots, m. Then the list of nonzero hkh_k in order, followed by QmQ_m if it is nonzero, is an expansion with value x+∑kekx + \sum_k e_k.

Proof. Value. Qk+hk=Qk−1+ekQ_k + h_k = Q_{k-1} + e_k for each kk. Summing these equations gives Qm+∑khk=x+∑kekQ_m + \sum_k h_k = x + \sum_k e_k.

Nonoverlap. Let gk=min⁡(ϵ(Qk−1),ϵ(ek))g_k = \min(\epsilon(Q_{k-1}), \epsilon(e_k)), a power of two. By Lemma B, QkQ_k and hkh_k are multiples of gkg_k. We show by induction on kk that for all i<ki < k,

(∗)∣hi∣<ϵ(ek)and∣hi∣<ϵ(Qk−1).(\ast)\qquad |h_i| < \epsilon(e_k) \quad\text{and}\quad |h_i| < \epsilon(Q_{k-1}).

The first part holds for every k>ik > i, by Lemmas A and C: ∣hi∣≤∣ei∣≤∑l<k∣el∣<ϵ(ek)|h_i| \le |e_i| \le \sum_{l<k}|e_l| < \epsilon(e_k). For the second part with k=i+1k = i + 1, Lemma A gives ∣hi∣<ϵ(Qi)|h_i| < \epsilon(Q_i), or hi=0h_i = 0. For k>i+1k > i + 1, the induction hypothesis for k−1k - 1 gives ∣hi∣<gk−1|h_i| < g_{k-1}, and gk−1g_{k-1} divides Qk−1Q_{k-1}, so ϵ(Qk−1)≥gk−1>∣hi∣\epsilon(Q_{k-1}) \ge g_{k-1} > |h_i|.

From (∗)(\ast), ∣hi∣<gk≤ϵ(hk)|h_i| < g_k \le \epsilon(h_k) whenever i<ki < k and hk≠0h_k \ne 0. The same argument with k=m+1k = m + 1, using gm+1:=ϵ(Qm)g_{m+1} := \epsilon(Q_m), gives ∣hi∣<ϵ(Qm)|h_i| < \epsilon(Q_m) for i<mi < m. Lemma A gives ∣hm∣<ϵ(Qm)|h_m| < \epsilon(Q_m). So every pair of consecutive components of the output, after the zeros are removed, satisfies the expansion condition. □\square

Theorem 2 (rounding). Let e1,…,eme_1, \ldots, e_m be a nonempty expansion with value SS. Lines L8 to L11 return RN(S)\mathrm{RN}(S), the nearest float with ties to even.

Proof. Write σj=ej+⋯+em\sigma_j = e_j + \cdots + e_m. Every ele_l with l≥jl \ge j is a multiple of ϵ(ej)\epsilon(e_j), because ϵ(el)\epsilon(e_l) increases with ll.

Exact phase. Suppose that before eje_j is processed, s=σj+1s = \sigma_{j+1}. This is true at the start. σj+1\sigma_{j+1} is a nonzero multiple of ϵ(ej+1)\epsilon(e_{j+1}) (Lemma C), so ∣s∣≥ϵ(ej+1)>∣ej∣|s| \ge \epsilon(e_{j+1}) > |e_j|, and Fast2Sum is exact. If its error is 00, then s=σjs = \sigma_j and the invariant continues. If the loop runs to the end, then s=σ1=Ss = \sigma_1 = S is a float, L10 does nothing, and the result is SS.

First inexact step. Otherwise the loop breaks at some jj with s=RN(σj)s = \mathrm{RN}(\sigma_j) and t=σj−s≠0t = \sigma_j - s \ne 0. Let R=∑l<jelR = \sum_{l < j} e_l, so S=s+t+RS = s + t + R. By Lemma B with g=ϵ(ej)g = \epsilon(e_j), tt is a nonzero multiple of ϵ(ej)\epsilon(e_j). By Lemma C, ∣R∣<ϵ(ej)≤∣t∣|R| < \epsilon(e_j) \le |t|, and if j>1j > 1 then R≠0R \ne 0 has the sign of ej−1e_{j-1}. Let DD be the distance from ss to the next float in the direction of tt. It is a power of two, and ∣t∣≤D/2|t| \le D/2 because ss is nearest to s+ts + t. D/2≥∣t∣≥ϵ(ej)D/2 \ge |t| \ge \epsilon(e_j) is a power of two, so it is a multiple of ϵ(ej)\epsilon(e_j). Line L10 computes 2t2t exactly. Also, fl(s+2t)−s\mathrm{fl}(s + 2t) - s is computed exactly, since it is 00 or the float ±D\pm D, the difference of two adjacent floats.

  • Case ∣t∣<D/2|t| < D/2. Then ∣t∣≤D/2−ϵ(ej)|t| \le D/2 - \epsilon(e_j), so t+Rt + R has the sign of tt and ∣t+R∣<D/2|t + R| < D/2. SS lies strictly between ss and the midpoint in that direction, so RN(S)=s\mathrm{RN}(S) = s. In L10, s+2ts + 2t lies strictly between ss and its neighbour, so fl(s+2t)−s∈{0,±D}\mathrm{fl}(s + 2t) - s \in \{0, \pm D\} differs from 2t2t, and ss is kept.
  • Case ∣t∣=D/2|t| = D/2, j=1j = 1. Then S=s+tS = s + t is a midpoint, and s=RN(σ1)s = \mathrm{RN}(\sigma_1) was chosen by ties to even. L10 does not fire.
  • Case ∣t∣=D/2|t| = D/2, j>1j > 1, RR with the sign of tt. Then D/2<∣t+R∣<D/2+ϵ(ej)≤DD/2 < |t + R| < D/2 + \epsilon(e_j) \le D. SS is past the midpoint, so RN(S)=s+2t\mathrm{RN}(S) = s + 2t, the neighbour. In L10, s+2ts + 2t is a float, so fl(s+2t)−s=2t\mathrm{fl}(s + 2t) - s = 2t, and the step is taken.
  • Case ∣t∣=D/2|t| = D/2, j>1j > 1, RR with the opposite sign. Then 0<∣t+R∣<D/20 < |t + R| < D/2, so RN(S)=s\mathrm{RN}(S) = s. The sign test in L10 fails, and ss is kept.

In every case the result is RN(S)\mathrm{RN}(S). □\square

Corollary. SHEWCHUK-SUM returns RN(x1+⋯+xn)\mathrm{RN}(x_1 + \cdots + x_n). Proof. The empty list is an expansion with value 0. By Theorem 1 and induction on ii, after step ii the list ee is an expansion with value x1+⋯+xix_1 + \cdots + x_i. If it is empty, the sum is 00 and the result is 00. Otherwise Theorem 2 applies. ■\blacksquare

Back to Shewchuk expansion arithmetic