Algorithm Search⌘K

Proofs: Kahan summation

These are the human proofs of Kahan summation, at full length.

Kahan summation has error at most (2u + (n + 4)u^2) times the sum of absolute values full

This proof shows: correctness.

Setting. Binary floating-point arithmetic with precision p≥9p \ge 9, subnormal numbers, round to nearest with any tie rule, and no overflow. The unit roundoff is u=2−pu = 2^{-p}, so u≤2−9u \le 2^{-9}. Every addition and subtraction satisfies fl(a±b)=(a±b)(1+ϵ)\mathrm{fl}(a \pm b) = (a \pm b)(1 + \epsilon) with ∣ϵ∣≤u|\epsilon| \le u; if the result underflows, the operation is exact. For a float xx, exe_x is its exponent (2ex≤∣x∣<2ex+12^{e_x} \le |x| < 2^{e_x + 1} for normal xx, and ex=emin⁡e_x = e_{\min} for subnormal xx and for 00).

The iteration. Let s0=c0=0s_0 = c_0 = 0 and, for i=1,…,ni = 1, \ldots, n,

yi=fl(xi−ci−1),ti=fl(si−1+yi),ci=fl(fl(ti−si−1)−yi),si=ti.y_i = \mathrm{fl}(x_i - c_{i-1}),\quad t_i = \mathrm{fl}(s_{i-1} + y_i),\quad c_i = \mathrm{fl}(\mathrm{fl}(t_i - s_{i-1}) - y_i),\quad s_i = t_i.

The algorithm returns s^=sn\hat s = s_n. Let A=∑i=1n∣xi∣A = \sum_{i=1}^n |x_i| and s=∑i=1nxis = \sum_{i=1}^n x_i.

Hypothesis (H). For every ii, si−1=0s_{i-1} = 0 or esi−1≥eyie_{s_{i-1}} \ge e_{y_i}.

Theorem. Under (H) and nu≤1/10nu \le 1/10, ∣s^−s∣≤(2u+(n+4)u2)A|\hat s - s| \le \bigl(2u + (n + 4)u^2\bigr) A.

Step 1 (the correction is exact). Fix ii. If si−1=0s_{i-1} = 0, then ti=yit_i = y_i, fl(ti−0)=yi\mathrm{fl}(t_i - 0) = y_i and ci=0=ti−(si−1+yi)c_i = 0 = t_i - (s_{i-1} + y_i). Otherwise esi−1≥eyie_{s_{i-1}} \ge e_{y_i}, and the theorem on Fast2Sum applied to a=si−1a = s_{i-1}, b=yib = y_i says that z=fl(ti−si−1)=ti−si−1z = \mathrm{fl}(t_i - s_{i-1}) = t_i - s_{i-1} exactly and fl(yi−z)=yi−z\mathrm{fl}(y_i - z) = y_i - z exactly, with ∣yi−z∣≤12ulp(ti)|y_i - z| \le \tfrac12\mathrm{ulp}(t_i). Line L6 computes fl(z−yi)=−fl(yi−z)\mathrm{fl}(z - y_i) = -\mathrm{fl}(y_i - z), because rounding to nearest is symmetric. So in both cases

ci=ti−(si−1+yi)exactly,(1)c_i = t_i - (s_{i-1} + y_i) \quad\text{exactly}, \tag{1}

and ∣ci∣≤12ulp(ti)|c_i| \le \tfrac12\mathrm{ulp}(t_i). If tit_i is normal, 12ulp(ti)≤u∣ti∣\tfrac12\mathrm{ulp}(t_i) \le u|t_i|. If tit_i is subnormal or zero, the addition si−1+yis_{i-1} + y_i is exact, so ci=0c_i = 0. Hence

∣ci∣≤u∣si∣.(2)|c_i| \le u|s_i|. \tag{2}

Step 2 (telescoping). Write yi=(xi−ci−1)(1+ϵi)y_i = (x_i - c_{i-1})(1 + \epsilon_i) with ∣ϵi∣≤u|\epsilon_i| \le u. By (1), si−ci=si−1+yi=si−1−ci−1+xi+ϵi(xi−ci−1)s_i - c_i = s_{i-1} + y_i = s_{i-1} - c_{i-1} + x_i + \epsilon_i(x_i - c_{i-1}). Let Di=si−ci−∑j≤ixjD_i = s_i - c_i - \sum_{j \le i} x_j; then D0=0D_0 = 0 and Di=Di−1+ϵi(xi−ci−1)D_i = D_{i-1} + \epsilon_i(x_i - c_{i-1}), so

s^−s=sn−∑j=1nxj=cn+∑i=1nϵi(xi−ci−1).(3)\hat s - s = s_n - \sum_{j=1}^n x_j = c_n + \sum_{i=1}^n \epsilon_i (x_i - c_{i-1}). \tag{3}

Step 3 (bounding the partial sums). Let S=max⁡1≤i≤n∣si∣S = \max_{1 \le i \le n} |s_i|. The same identity at step ii gives si=∑j≤ixj+ci+Dis_i = \sum_{j \le i} x_j + c_i + D_i with ∣Di∣≤u∑j≤i∣xj∣+u∑j<i∣cj∣|D_i| \le u\sum_{j \le i}|x_j| + u\sum_{j < i}|c_j|. By (2), ∣cj∣≤uS|c_j| \le uS, so

∣si∣≤A+uS+uA+(i−1)u2S≤(1+u)A+wS,w=u+(n−1)u2.|s_i| \le A + uS + uA + (i - 1)u^2 S \le (1 + u)A + wS, \qquad w = u + (n - 1)u^2.

Taking the maximum over ii, S(1−w)≤(1+u)AS(1 - w) \le (1 + u)A. Since nu≤1/10nu \le 1/10, w≤u(1+nu)≤1.1u<1/2w \le u(1 + nu) \le 1.1u < 1/2, so S≤(1+u)A/(1−w)S \le (1 + u)A/(1 - w).

Step 4 (the bound). From (3) and (2), with c0=0c_0 = 0,

∣s^−s∣≤u∣sn∣+uA+u∑i=1n−1∣ci∣≤uA+uS+(n−1)u2S=uA+wS≤uA+(1+u)w1−wA.|\hat s - s| \le u|s_n| + uA + u\sum_{i=1}^{n-1}|c_i| \le uA + uS + (n - 1)u^2 S = uA + wS \le uA + \frac{(1 + u)w}{1 - w}A.

For 0≤w≤1/20 \le w \le 1/2, 1/(1−w)≤1+2w1/(1 - w) \le 1 + 2w, so (1+u)w/(1−w)≤w+uw+2w2+2uw2(1 + u)w/(1 - w) \le w + uw + 2w^2 + 2uw^2. Write w=uaw = ua with a=1+(n−1)u≤1.1a = 1 + (n - 1)u \le 1.1. Then uw=u2a≤1.1u2uw = u^2 a \le 1.1u^2, 2w2=2u2a2≤2.42u22w^2 = 2u^2a^2 \le 2.42u^2 and 2uw2≤2.42u3≤0.01u22uw^2 \le 2.42u^3 \le 0.01u^2, using u≤2−9u \le 2^{-9}. So

(1+u)w1−w≤u+(n−1)u2+3.53u2≤u+(n+3)u2,\frac{(1 + u)w}{1 - w} \le u + (n - 1)u^2 + 3.53u^2 \le u + (n + 3)u^2,

and ∣s^−s∣≤(2u+(n+3)u2)A≤(2u+(n+4)u2)A|\hat s - s| \le (2u + (n + 3)u^2)A \le (2u + (n + 4)u^2)A. □\square

Remarks. The proof uses round to nearest twice: in the Fast2Sum theorem and in the symmetry fl(−v)=−fl(v)\mathrm{fl}(-v) = -\mathrm{fl}(v). Without (H), line L6 need not be exact: for si−1=2−60s_{i-1} = 2^{-60} and yi=1y_i = 1, ti=1t_i = 1, fl(ti−si−1)=1\mathrm{fl}(t_i - s_{i-1}) = 1 and ci=0c_i = 0, while the true error is −2−60-2^{-60}. The general bound (2u+O(nu2))A(2u + O(nu^2))A without (H) is proved by Knuth (TAOCP Volume 2, Section 4.2.2) and by Goldberg (Theorem 8), with a case analysis of the steps where the term is larger than the running sum.

Back to Kahan summation