Short answer: Kahan summation adds a sequence of floating-point numbers while keeping a correction for the low bits the last addition lost. Each step subtracts the running correction from the next input, adds that adjusted value to the sum, and stores the bits that did not land in the sum as the next correction. The error is on the order of machine epsilon times the sum of absolute values, plus a quadratic term in n times epsilon squared, rather than a term that grows like n epsilon. The parentheses in the correction update are part of the algorithm.
What the problem actually asks
A floating-point sum of many small values into a large accumulator rounds away the small values. Adding a million unit values into a sum that has already reached 2^{30}, in single precision, can add nothing at all. The lost part is not random noise. It is the difference between the exact sum of the two operands and the rounded result, and a careful update can feed that difference into the next addition.
Kahan’s running correction holds that difference. Over a long sum the corrections accumulate the bits that each individual addition would have dropped on the floor. The algorithm does not make floating-point addition associative, and it does not recover a mathematically exact sum once the total rounding exceeds what the correction variable can represent.
What the algorithm guarantees
In binary floating-point arithmetic with unit roundoff ε, when n ε is less than 1, the computed sum differs from the exact sum by at most about (2ε + O(n ε²)) times the sum of the absolute values. Naive left-to-right summation can be off by about n ε times that quantity. Time is a small constant number of floating-point operations per input, Θ(n) overall. Extra memory is the correction variable.
The bound assumes the operations round to nearest in the standard way and that the compiler emits the subtraction that recovers the correction. An optimizer that reassociates the expression (t − sum) − y into t − (sum + y) destroys it. A wider accumulator, when the hardware has one, is a different and often stronger fix. Pairwise summation is another algorithm with a stronger dependence on the addition tree and a different bound.
The working steps
- Set the sum and the correction c to 0.
- For each input x, set y = x − c. This folds the leftover of the previous addition into the new term.
- Set t = sum + y. This is the rounded new sum.
- Set c = (t − sum) − y. The parentheses recover the negative of the part of y that t could not hold.
- Set sum = t. After the last input, sum is the result. Do not drop c by algebraic simplification.
The improved constant is for summation. It does not repair a cancellation inside a dot product or an ill-conditioned subtraction that happens before the numbers reach this loop. Pairwise summation is the divide-and-conquer alternative. Kahan summation is the compensated left-to-right sum.
Approaches that look simpler and fail
| Approach | Why it fails when the correction line is algebraically simplified |
|---|---|
| Rewrite (t − sum) − y as t − sum − y and let a compiler reassociate it | In exact arithmetic the correction is zero. The algorithm depends on the rounding of t = sum + y happening before the subtraction. Reassociation removes the information. |
| Add the inputs in a tree and still quote the Kahan constant | A tree sum is pairwise summation. It has its own bound. The running correction is defined for the left-to-right loop. |
| Expect an exact integer answer from a float sum of large integers | Once the integers exceed the mantissa, the input itself is not exact. Compensation cannot restore bits that never entered x. |
| Ignore c on the last step by returning t only after clearing the correction | The last correction has already been folded into the next y when there is a next term. Returning sum at the end is correct. Clearing c early and adding it twice is the bug to avoid. |
A wider accumulator is a hardware or long-double sum. Pairwise summation splits the array. Kahan summation is the running correction.
A working example
This Python fragment keeps the bits a floating-point addition could not store and feeds them into the next term. Summing 0.1, one thousand times, the compensated total is 100.0 at this printing precision.
def kahan(values):
total = c = 0.0
for x in values:
y = x - c
t = total + y
c = (t - total) - y
total = t
return total
values = [0.1] * 1000
got = kahan(values)
assert abs(got - 100.0) < 1e-9
print(f"{got:.1f}")
Where it is used
Kahan summation is the compensated running total in a long reduction of floating-point measurements. A plain sum drifts because small terms are lost against a large accumulator. Pairwise summation is the other stable reduction numerical libraries offer.
When you can drop this algorithm
Drop compensated floating-point addition when the values are integers that fit in an integer type. Add them in integer arithmetic. Keep Kahan when the inputs are floating-point and the sum is long enough for n ε to matter.
Why this stays published
We publish this so a search for Kahan summation returns the correction term (t − sum) − y and the error bound that stays near machine epsilon. Kahan summation adds floating-point values while feeding the rounded-off part of each addition back in as a correction, keeping the error near machine epsilon rather than near n epsilon.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
