Algorithm Search⌘K

Proofs: Reproducible floating-point summation

These are the human proofs of Reproducible floating-point summation, at full length.

K-fold summation is exact in every fold, hence order independent, with a bounded error full

This proof shows: correctness.

Setting. Binary floating point with precision p≥4p \ge 4 and round to nearest under any tie rule. x1,…,xnx_1, \ldots, x_n are finite floats, not all zero, with 2≤2n≤2p−12 \le 2n \le 2^{p-1}. Let m=max⁡i∣xi∣m = \max_i |x_i|, c=⌈log⁡2n⌉+1c = \lceil \log_2 n \rceil + 1 and Mj=2ajM_j = 2^{a_j} with aj=⌈log⁡2m⌉+c+(j−1)(c−p)a_j = \lceil \log_2 m \rceil + c + (j - 1)(c - p). Assume no overflow (M1≤2emax⁡M_1 \le 2^{e_{\max}}) and aK≥emin⁡+1a_K \ge e_{\min} + 1, so that every unit Mj2−pM_j 2^{-p} is at least the smallest subnormal 2emin⁡−p+12^{e_{\min} - p + 1} and Mj/2M_j/2 is normal. (The implementation keeps this second condition by clamping, which is treated in Step 6.) We use two facts.

  • (F1) Sterbenz. If a,ba, b are floats with b/2≤a≤2bb/2 \le a \le 2b, then a−ba - b is a float.
  • (F2) If a,ba, b are floats and s=fl(a+b)s = \mathrm{fl}(a + b), then a+b−sa + b - s is a float. This holds with or without underflow.

Theorem. Let vi(1)=xiv^{(1)}_i = x_i, and for j=1,…,Kj = 1, \ldots, K let qi(j)=fl(fl(Mj+vi(j))−Mj)q^{(j)}_i = \mathrm{fl}(\mathrm{fl}(M_j + v^{(j)}_i) - M_j) and vi(j+1)=fl(vi(j)−qi(j))v^{(j+1)}_i = \mathrm{fl}(v^{(j)}_i - q^{(j)}_i). Then:

  1. qi(j)=s−Mjq^{(j)}_i = s - M_j and vi(j+1)=vi(j)−qi(j)v^{(j+1)}_i = v^{(j)}_i - q^{(j)}_i exactly, where s=fl(Mj+vi(j))s = \mathrm{fl}(M_j + v^{(j)}_i).
  2. For every order of ii, line L9 computes Tj=∑iqi(j)T_j = \sum_i q^{(j)}_i exactly. So TjT_j depends only on the multiset {xi}\{x_i\}. The same holds when the index set is partitioned, each part is summed separately, and the part sums are added in any order.
  3. ∣∑ixi−∑jTj∣≤nMK2−p<2nm (4n2−p)K\bigl|\sum_i x_i - \sum_j T_j\bigr| \le n M_K 2^{-p} < 2nm\,(4n 2^{-p})^{K}.

Step 1 (input bound). We show ∣vi(j)∣≤Mj/(2n)|v^{(j)}_i| \le M_j / (2n) for all j≤Kj \le K. For j=1j = 1, m≤2⌈log⁡2m⌉m \le 2^{\lceil \log_2 m \rceil} and 2n≤2c2n \le 2^{c}, so 2nm≤M12nm \le M_1. For j≥1j \ge 1, Step 3 below gives ∣vi(j+1)∣≤Mj2−p=Mj+12−c≤Mj+1/(2n)|v^{(j+1)}_i| \le M_j 2^{-p} = M_{j+1} 2^{-c} \le M_{j+1}/(2n). The argument is an induction on jj, in which Steps 2 and 3 use only the bound for the current jj.

Step 2 (exact extraction). Fix jj and ii, and write M=MjM = M_j, v=vi(j)v = v^{(j)}_i. Since ∣v∣≤M/(2n)≤M/2|v| \le M/(2n) \le M/2, we have M/2≤M+v≤3M/2M/2 \le M + v \le 3M/2. The numbers M/2M/2 and 3M/23M/2 are floats, because p≥2p \ge 2 and M/2M/2 is normal. Rounding is monotone, so s=fl(M+v)∈[M/2,3M/2]s = \mathrm{fl}(M + v) \in [M/2, 3M/2]. By (F1), s−Ms - M is a float, so q=s−Mq = s - M exactly. Then v−q=(M+v)−sv - q = (M + v) - s, which is a float by (F2), so fl(v−q)=v−q\mathrm{fl}(v - q) = v - q. This proves part 1.

Step 3 (sizes and grid). Every float in [M/2,2M)[M/2, 2M) is a multiple of ulp(M/2)=M2−p\mathrm{ulp}(M/2) = M 2^{-p}, and so is MM. Hence q=s−Mq = s - M is an integer multiple of U=M2−pU = M 2^{-p}. The residual is the rounding error of M+vM + v, so ∣v−q∣≤12ulp(s)≤12⋅2M2−p=U|v - q| \le \tfrac12 \mathrm{ulp}(s) \le \tfrac12 \cdot 2M 2^{-p} = U, because s<2Ms < 2M gives ulp(s)≤2U\mathrm{ulp}(s) \le 2U. So ∣q∣≤∣v∣+U≤M/(2n)+U|q| \le |v| + U \le M/(2n) + U.

Step 4 (exact accumulation). Let AA be any subset of the indices. Then ∣∑i∈Aqi(j)∣≤n(M/(2n)+U)=M/2+nU≤M\bigl|\sum_{i \in A} q^{(j)}_i\bigr| \le n\bigl(M/(2n) + U\bigr) = M/2 + nU \le M, since nU=nM2−p≤M/2nU = n M 2^{-p} \le M/2 by 2n≤2p−12n \le 2^{p-1}. The sum is an integer multiple of UU of magnitude at most M=2pUM = 2^{p} U. Every such number is a float: it is kUkU with ∣k∣≤2p|k| \le 2^{p}, and U≥2emin⁡−p+1U \ge 2^{e_{\min} - p + 1}. Every partial sum formed in L9, in any order and over any partition, is such a subset sum. So every addition in L9 is exact, and the final TjT_j equals ∑iqi(j)\sum_i q^{(j)}_i. This proves part 2.

Step 5 (error). By part 1, xi=∑j=1Kqi(j)+vi(K+1)x_i = \sum_{j=1}^{K} q^{(j)}_i + v^{(K+1)}_i for each ii. Summing over ii, ∑ixi=∑jTj+E\sum_i x_i = \sum_j T_j + E with E=∑ivi(K+1)E = \sum_i v^{(K+1)}_i, and by Step 3, ∣E∣≤nMK2−p|E| \le n M_K 2^{-p}. For the explicit form, 2⌈log⁡2m⌉<2m2^{\lceil \log_2 m \rceil} < 2m and 2c<4n2^{c} < 4n give M1<8nmM_1 < 8nm, and 2c−p<4n2−p2^{c - p} < 4n 2^{-p}. So MK<8nm(4n2−p)K−1M_K < 8nm (4n 2^{-p})^{K-1}, and nMK2−p<2nm(4n2−p)Kn M_K 2^{-p} < 2nm (4n 2^{-p})^{K}. This proves part 3.

Step 6 (the clamped bottom level). The implementation never uses an exponent below emin⁡+1e_{\min} + 1. If aja_j would fall below it, it uses aj=emin⁡+1a_j = e_{\min} + 1 instead. A larger boundary keeps Step 1 true, since ∣v∣≤Mj−12−p≤2emin⁡+1/(2n)|v| \le M_{j-1}2^{-p} \le 2^{e_{\min}+1}/(2n) whenever aj−1+c−p≤emin⁡+1a_{j-1} + c - p \le e_{\min} + 1. With M=2emin⁡+1M = 2^{e_{\min} + 1} the unit is U=2emin⁡−p+1U = 2^{e_{\min} - p + 1}, the smallest subnormal. After that level every residual is 00 or ±U\pm U. The final level uses M=0M = 0, so q=vq = v exactly and v(K+1)=0v^{(K+1)} = 0. Its partial sums are multiples of UU of magnitude at most nU≤2pUnU \le 2^{p} U, so they are floats. Parts 1 and 2 hold for this level, and part 3 holds with E=0E = 0.

Corollary. The returned value fl(T1+fl(T2+⋯ ))\mathrm{fl}(T_1 + \mathrm{fl}(T_2 + \cdots)) is computed from the TjT_j in a fixed order. So it is the same for every order and partition of the input. By the standard model, ∣R−∑jTj∣≤γK−1∑j∣Tj∣|R - \sum_j T_j| \le \gamma_{K-1}\sum_j |T_j|, and with part 3,

∣R−∑ixi∣≤γK−1∑j∣Tj∣+nMK2−p.■\Bigl|R - \sum_i x_i\Bigr| \le \gamma_{K-1}\sum_j |T_j| + n M_K 2^{-p}. \qquad \blacksquare

Back to Reproducible floating-point summation