On this page
Add 10⁻¹⁶ to 1 ten million times in double precision and the true answer is 1.000000001. The simple loop returns exactly 1, with no warning.
Why does naive summation of 1 + 10⁻¹⁶ lose everything?
A double-precision number keeps 53 binary digits, so with round-to-nearest the largest relative rounding error, called the unit roundoff, is 2⁻⁵³ ≈ 1.11 × 10⁻¹⁶ (Machine epsilon, Wikipedia). The gap between 1 and the next number a double can hold is 2⁻⁵² ≈ 2.22 × 10⁻¹⁶, and anything smaller than half that gap, added to 1, rounds back to 1.
Since 10⁻¹⁶ is less than 1.11 × 10⁻¹⁶, 1 + 10⁻¹⁶ computes to exactly 1, and every later addition repeats the same loss. New to floating-point rounding? Start with our explainer on why 0.1 + 0.2 ≠ 0.3.
How does Kahan compensated summation work?
William Kahan published the method in Communications of the ACM in January 1965 (Kahan, 1965). The idea: after every addition, work out what was rounded off and take it away from the next number before adding. David Goldberg's tutorial gives the algorithm in this form (Goldberg, 1991):
Start with S = x₁ and C = 0.
For the next number xⱼ, set Y = xⱼ − C (the new number, corrected by the last error).
Set T = S + Y. This addition rounds.
Set C = (T − S) − Y. In exact arithmetic C would be 0; in floating point it is minus the part of Y that T failed to absorb.
Set S = T and go back to step 2.
Step 4 does the work: T − S is what actually got into the sum, so subtracting Y leaves the lost piece. A compiler that "simplifies" this to 0 breaks the method, which is why the Wikipedia article on Kahan summation warns about overly aggressive optimising compilers.
Can you do Kahan summation by hand?
Yes. Pretend a calculator keeps only 4 significant digits. Start at 1000 and add 0.4 five times. The exact answer is 1002.
Naive: 1000 + 0.4 = 1000.4 rounds to 1000, every time. The answer stays 1000.
Kahan, starting from S = 1000 and C = 0:
Y = 0.4 − 0 = 0.4. T = 1000.4, rounds to 1000. C = (1000 − 1000) − 0.4 = −0.4. S = 1000.
Y = 0.4 − (−0.4) = 0.8. T = 1000.8, rounds to 1001. C = (1001 − 1000) − 0.8 = 0.2. S = 1001.
Y = 0.4 − 0.2 = 0.2. T = 1001.2, rounds to 1001. C = (1001 − 1001) − 0.2 = −0.2. S = 1001.
Y = 0.4 − (−0.2) = 0.6. T = 1001.6, rounds to 1002. C = (1002 − 1001) − 0.6 = 0.4. S = 1002.
Y = 0.4 − 0.4 = 0. T = 1002. C = 0. S = 1002.
Kahan returns 1002, exactly right. At every step S − C equals the exact running total (1000.4, 1000.8, 1001.2, 1001.6, 1002). To practise, redo it adding 0.3 and check that S − C stays exact.
How accurate is Kahan summation compared with naive and pairwise?
Goldberg's Theorem 8 states that the computed Kahan sum equals Σ xⱼ(1 + δⱼ) + O(Nε²) Σ|xⱼ|, with every |δⱼ| ≤ 2ε (Goldberg, 1991). In words, each number is disturbed by about 2ε at most, against as much as nε in the simple loop; the O(Nε²) term is negligible unless N approaches 1/ε.
The worst-case error of naive summation grows as O(εn), pairwise summation (split the list in halves, sum each half, add the two) grows as O(ε log n), and Kahan summation stays at roughly O(ε), each multiplied by the condition number Σ|xᵢ| / |Σxᵢ| (Pairwise summation, Wikipedia, summarising Higham, 1993).
Plug in the ten-million-term sum, with ε ≈ 1.11 × 10⁻¹⁶ and all terms positive, so the condition number is 1:
Naive: about nε ≈ 10⁷ × 1.11 × 10⁻¹⁶ ≈ 1.1 × 10⁻⁹, about as large as the entire 10⁻⁹ we were trying to add.
Pairwise: about ε log₂ n ≈ 23 × 1.11 × 10⁻¹⁶ ≈ 2.6 × 10⁻¹⁵.
Kahan: about 2ε ≈ 2.2 × 10⁻¹⁶, so the 10⁻⁹ survives with room to spare.
The price is four additions or subtractions per term instead of one. Nicholas Higham analysed five summation methods and concluded that no one method is uniformly more accurate than the others (Higham, 1993). Julia's default sum is pairwise (Wikipedia); Python's math.fsum tracks multiple intermediate partial sums (Python documentation).
When should you use compensated summation?
Use it when:
You add many numbers into one total: long averages, a simulation stepping through time, numerical integration.
The terms are much smaller than the running total, like 0.4 against 1000.
You need the last digits right, for example when two totals should agree.
It cannot rescue cancellation: if signs are mixed and the true sum is tiny, the condition number is large and the relative error can still be large.
For more work with sums, try these sequences and series JEE Main questions or our worked example of Abel summation by parts.
If rounding still feels shaky, the Fermi Concept Library covers every concept in mathematics and computing. For practice, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning.



