Numerical analysis
Floating-point arithmetic
Error reduction
Computational methods
Summation algorithm

Kahan summation

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Kahan summation is a compensated summation algorithm that reduces floating-point rounding error when you add many values together. It does not make floating-point arithmetic exact, but it often gives a noticeably better result than naive left-to-right accumulation at very low extra cost.

Why Ordinary Summation Loses Accuracy

Floating-point numbers have limited precision. When you add a tiny number to a much larger one, the tiny contribution can disappear during rounding.

A classic example is:

python
values = [1e16, 1.0, -1e16]
print(sum(values))

Mathematically the result should be 1.0, but naive floating-point accumulation often produces 0.0.

The problem is not Python specifically. It is the finite precision of floating-point representation.

The Idea Behind Kahan Summation

Kahan summation keeps a second variable, often called the compensation term, that remembers the small low-order bits lost in previous additions.

At each step:

  1. subtract the compensation from the new value
  2. add the corrected value to the running sum
  3. compute the new rounding error
  4. store that error as the next compensation

This gives the algorithm a way to "re-inject" some of the information that naive summation would lose permanently.

A Simple Python Implementation

python
1def kahan_sum(values):
2    total = 0.0
3    c = 0.0
4
5    for x in values:
6        y = x - c
7        t = total + y
8        c = (t - total) - y
9        total = t
10
11    return total
12
13
14values = [1e16, 1.0, -1e16]
15print("naive :", sum(values))
16print("kahan :", kahan_sum(values))

On many systems, the Kahan version returns the expected 1.0 while the naive version does not.

What the Compensation Variable Means

The compensation term is not the exact mathematical error. It is the rounding loss from the previous addition step, captured in a way that can be used on the next step.

That is why the update:

python
c = (t - total) - y

looks unusual at first glance. It reconstructs the low-order bits that were swallowed by floating-point rounding when y was added to total.

Without that variable, those bits would simply vanish.

When Kahan Helps Most

Kahan summation is especially useful when:

  • summing a long list of values
  • magnitudes vary widely
  • cancellation happens often
  • small errors accumulate into visible drift

Common examples include:

  • statistics
  • simulation
  • finance
  • physics
  • iterative numerical methods

If all numbers are similar in magnitude and the list is short, the improvement may be small. But once the sequence gets long or numerically awkward, compensated summation becomes more attractive.

Kahan Is Better, Not Perfect

It is important to be precise about what Kahan does not guarantee:

  • it does not make floating point exact
  • it does not outperform every advanced summation method in every case
  • it still depends on data order

There are stronger techniques, such as pairwise summation and more advanced compensated schemes, but Kahan is a great middle ground because it is simple and cheap.

Compare with Naive Summation on Mixed Magnitudes

Here is a slightly larger example:

python
1values = [1e8] + [1e-5] * 1_000_000 + [-1e8]
2
3print("naive :", sum(values))
4print("kahan :", kahan_sum(values))

The exact result should be about 10.0. Naive summation can drift noticeably, while Kahan typically tracks the small contributions much better.

This is the kind of scenario where compensated summation earns its keep.

Performance Tradeoff

Kahan adds a few arithmetic operations per element. For most business and scientific workloads, that overhead is modest compared with the benefit in accuracy.

Still, if you are summing massive arrays inside tight numeric kernels, it is worth benchmarking because:

  • vectorized library routines may use other stable techniques
  • the memory access pattern may dominate
  • hardware and compiler behavior can matter

The algorithm is simple enough that you should treat it as a tool to apply intentionally, not as a universal default for every sum.

Common Pitfalls

  • Assuming Kahan produces exact arithmetic instead of improved floating-point behavior.
  • Using it without understanding whether the workload actually has a numerical stability problem.
  • Forgetting that summation order still matters even with compensation.
  • Comparing it only against one tiny toy example and generalizing too much.
  • Replacing optimized array-library reductions blindly without measuring the cost.

Summary

  • Kahan summation reduces floating-point error by tracking lost low-order bits in a compensation term.
  • It is especially useful when summing many numbers with very different magnitudes.
  • The implementation is simple and often much more accurate than naive accumulation.
  • It improves stability, but it does not make floating-point arithmetic exact.
  • Use it when numerical error matters enough to justify a small extra cost per addition.

Course illustration
Course illustration

All Rights Reserved.