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 to input nput.length for i = 1 to input.length do // ''c'' is zero the first time around. var y = input - 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) // 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 for i = 1 to input.length do // ''c'' is zero the first time around. var y = input + c // ''sum'' + ''c'' is an approximation to the exact sum. (sum,c) = Fast2Sum(sum,y) // Next time around, the lost low part will be added to ''y'' in a fresh attempt. next i return sumWorked example
The algorithm does not mandate any specific choice ofsum
has attained the value 10000.0, and the next two values of input /code> 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 - 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 ' 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
A careful analysis of the errors in compensated summation is needed to appreciate its accuracy characteristics. While it is more accurate than naive summation, it can still give large relative errors for ill-conditioned sums.
Suppose that one is summing values , for . The exact sum is
: (computed with infinite precision).
With compensated summation, one instead obtains , where the error is bounded by
:
where is the machine precision of the arithmetic being employed (e.g. for IEEE standard double-precision floating point). Usually, the quantity of interest is the relative error , which is therefore bounded above by
:
In the expression for the relative error bound, the fraction is the condition number of the summation problem. Essentially, the condition number represents the ''intrinsic'' sensitivity of the summation problem to errors, regardless of how it is computed. The relative error bound of ''every'' ( backwards stable) summation method by a fixed algorithm in fixed precision (i.e. not those that use arbitrary-precision arithmetic, nor algorithms whose memory and time requirements change based on the data), is proportional to this condition number. An ''ill-conditioned'' summation problem is one in which this ratio is large, and in this case even compensated summation can have a large relative error. For example, if the summands are uncorrelated random numbers with zero mean, the sum is a random walk
In mathematics, a random walk, sometimes known as a drunkard's walk, is a stochastic process that describes a path that consists of a succession of random steps on some Space (mathematics), mathematical space.
An elementary example of a rand ...
, and the condition number will grow proportional to . On the other hand, for random inputs with nonzero mean the condition number asymptotes to a finite constant as . If the inputs are all non-negative
In mathematics, the sign of a real number is its property of being either positive, negative, or 0. Depending on local conventions, zero may be considered as having its own unique sign, having no sign, or having both positive and negative sign. ...
, then the condition number is 1.
Given a condition number, the relative error of compensated summation is effectively independent of . In principle, there is the that grows linearly with , but in practice this term is effectively zero: since the final result is rounded to a precision , the term rounds to zero, unless is roughly or larger. In double precision, this corresponds to an of roughly , much larger than most sums. So, for a fixed condition number, the errors of compensated summation are effectively , independent of .
In comparison, the relative error bound for naive summation (simply adding the numbers in sequence, rounding at each step) grows as multiplied by the condition number. This worst-case error is rarely observed in practice, however, because it only occurs if the rounding errors are all in the same direction. In practice, it is much more likely that the rounding errors have a random sign, with zero mean, so that they form a random walk; in this case, naive summation has a root mean square
In mathematics, the root mean square (abbrev. RMS, or rms) of a set of values is the square root of the set's mean square.
Given a set x_i, its RMS is denoted as either x_\mathrm or \mathrm_x. The RMS is also known as the quadratic mean (denote ...
relative error that grows as multiplied by the condition number.Manfred Tasche and Hansmartin Zeuner, ''Handbook of Analytic-Computational Methods in Applied Mathematics'', Boca Raton, FL: CRC Press, 2000. This is still much worse than compensated summation, however. However, if the sum can be performed in twice the precision, then is replaced by , and naive summation has a worst-case error comparable to the term in compensated summation at the original precision.
By the same token, the that appears in above is a worst-case bound that occurs only if all the rounding errors have the same sign (and are of maximal possible magnitude). In practice, it is more likely that the errors have random sign, in which case terms in are replaced by a random walk, in which case, even for random inputs with zero mean, the error grows only as (ignoring the term), the same rate the sum grows, canceling the factors when the relative error is computed. So, even for asymptotically ill-conditioned sums, the relative error for compensated summation can often be much smaller than a worst-case analysis might suggest.
Further enhancements
Neumaier introduced an improved version of Kahan algorithm, which he calls an "improved Kahan–Babuška algorithm", which also covers the case when the next term to be added is larger in absolute value than the running sum, effectively swapping the role of what is large and what is small. In pseudocode, the algorithm is:
function KahanBabushkaNeumaierSum(input)
var sum = 0.0
var c = 0.0 // A running compensation for lost low-order bits.
for i = 1 to input.length do
var t = sum + input if , sum, >= , input then
c += (sum - t) + input // If ''sum'' is bigger, low-order digits of ''input ' are lost.
else
c += (input - t) + sum // Else low-order digits of ''sum'' are lost.
endif
sum = t
next i
return sum + c // Correction only applied once in the very end.
This enhancement is similar to the Fast2Sum version of Kahan's algorithm with Fast2Sum replaced by 2Sum.
For many sequences of numbers, both algorithms agree, but a simple example due to Peters shows how they can differ: summing