ArticleslgStudy

computer science

Kahan summation algorithm

Kahan summation algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Kahan summation algorithm rather than just read about it. In short: In numerical analysis, the Kahan summation algorithm, also known as compensated summation, significantly reduces the numerical error in the total obtained by adding a sequence of finite-precision floating-point numbers, compared to the naive approach (a summation algorithm). This is done by keeping a separate running compensation (a variable to accumulate small errors), in effect extending the precision of the sum b…

Key takeaways

  • Kahan summation algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Kahan summation algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Kahan summation algorithm from memory before moving on to harder problems.

Reference excerpt

In numerical analysis, the Kahan summation algorithm, also known as compensated summation, significantly reduces the numerical error in the total obtained by adding a sequence of finite-precision floating-point numbers, compared to the naive approach (a summation algorithm). This is done by keeping a separate running compensation (a variable to accumulate small errors), in effect extending the precision of the sum by the precision of the compensation variable. In particular, simply summing n {\displaystyle n} numbers in sequence has a worst-case error that grows proportional to n {\displaystyle n} , and a root mean square error that grows as n {\displaystyle {\sqrt {n}}} for random inputs (the roundoff errors form a random walk). With compensated summation, using a compensation variable with sufficiently high precision the worst-case error bound is effectively independent of n {\displaystyle n} , so a large number of values can be summed with an error that only depends on the floating-point precision of the result. The algorithm is attributed to William Kahan; Ivo Babuška seems to have come up with a similar algorithm independently (hence Kahan–Babuška summation). Similar, earlier techniques are, for example, Bresenham's line algorithm, keeping track of the accumulated error in integer operations (although first documented around the same time) and the delta-sigma modulation.

The algorithm In pseudocode, the algorithm will be:

function KahanSum(input) // Prepare the accumulator. var sum = 0.0 // A running compensation for lost low-order bits. var c = 0.0 // The array input has elements indexed input[1] to input[input.length]. for i = 1 to input.length do // c is zero the first time around. var y = input[i] - c // Alas, sum is big, y small, so low-order digits of y are lost. var t = sum + y // (t - sum) cancels the high-order part of y; // subtracting y recovers negative (low part of y) c = (t - sum) - y // Algebraically, c should always be zero. Beware // overly-aggressive optimizing compilers! sum = t // Next time around, the lost low part will be added to y in a fresh attempt. next i

return sum

This algorithm can also be rewritten to use the Fast2Sum algorithm:

function KahanSum2(input) var sum = 0.0 var c = 0.0 for i = 1 to input.length do var y = input[i] + c // sum + c is an approximation to the exact sum. (sum,c) = Fast2Sum(sum,y) next i

return sum

Worked example The algorithm does not mandate any specific choice of radix, only for the arithmetic to "normalize floating-point sums before rounding or truncating". Computers typically use binary arithmetic, but to make the example easier to read, it will be given in decimal. Suppose we are using six-digit decimal floating-point arithmetic, sum has attained the value 10000.0, and the next two values of input[i] are 3.14159 and 2.71828. The exact result is 10005.85987, which rounds to 10005.9. With a plain summation, each incoming value would be aligned with sum, and many low-order digits would be lost (by truncation or rounding). The first result, after rounding, would be 10003.1. The second result would be 10005.81828 before rounding and 10005.8 after rounding. This is not correct. However, with compensated summation, we get the correctly rounded result of 10005.9. Assume that c has the initial value zero. Trailing zeros shown where they are significant for the six-digit floating-point number.

y = 3.14159 - 0.00000 y = input[i] - c t = 10000.0 + 3.14159 t = sum + y = 10003.14159 Normalization done, next round off to six digits. = 10003.1 Few digits from input[i] met those of sum. Many digits have been lost! c = (10003.1 - 10000.0) - 3.14159 c = (t - sum) - y (Note: Parenthesis must be evaluated first!) = 3.10000 - 3.14159 The assimilated part of y minus the original full y. = -0.0415900 Because c is close to zero, normalization retains many digits after the floating point. sum = 10003.1 sum = t

The sum is so large that only the high-order digits of the input numbers are being accumulated. But on the next step, c, an approximation of the running error, counteracts the problem.

y = 2.71828 - (-0.0415900) Most digits meet, since c is of a size similar to y. = 2.75987 The shortfall (low-order digits lost) of previous iteration successfully reinstated. t = 10003.1 + 2.75987 But still only few meet the digits of sum. = 10005.85987 Normalization done, next round to six digits. = 10005.9 Again, many digits have been lost, but c helped nudge the round-off. c = (10005.9 - 10003.1) - 2.75987 Estimate the accumulated error, based on the adjusted y. = 2.80000 - 2.75987 As expected, the low-order parts can be retained in c with no or minor round-off effects. = 0.0401300 In this iteration, t was a bit too high, the excess will be subtracted off in next iteration. sum = 10005.9 Exact result is 10005.85987, sum is correct, rounded to 6 digits.

The algorithm performs summation with two accumulators: sum holds the sum, and c accumulates the parts not assimilated into sum, to nudge the low-order part of sum the next time around. Thus the summation proceeds with "guard digits" in c, which is better than not having any, but is not as good as performing the calculations with double the precision of the input. However, simply increasing the precision of the calculations is not practical in general; if input is already in double precision, few systems supply quadruple precision, and if they did, input could then be in quadruple precision.

Accuracy

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Kahan summation algorithm

Start with the simplest possible case. Write down what Kahan summation algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Kahan summation algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Kahan summation algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Kahan summation algorithm

In research
Kahan summation algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Kahan summation algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Kahan summation algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer arithmetic, Floating point, Numerical analysis, so understanding it makes those chapters shorter.
In everyday life
Look for Kahan summation algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Kahan summation algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Kahan summation algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Kahan summation algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Kahan summation algorithm in simple terms?

In numerical analysis, the Kahan summation algorithm, also known as compensated summation, significantly reduces the numerical error in the total obtained by adding a sequence of finite-precision floating-point numbers, compared to the naive approach (a summation algorithm). This is done by keeping…

Why does Kahan summation algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Kahan summation algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Kahan summation algorithm.

Tags

  • Computer arithmetic
  • Floating point
  • Numerical analysis

Keep exploring