<s>
In	O
numerical	B-General_Concept
analysis	I-General_Concept
,	O
the	O
Kahan	B-Algorithm
summation	I-Algorithm
algorithm	I-Algorithm
,	O
also	O
known	O
as	O
compensated	B-Algorithm
summation	I-Algorithm
,	O
significantly	O
reduces	O
the	O
numerical	B-Algorithm
error	I-Algorithm
in	O
the	O
total	O
obtained	O
by	O
adding	O
a	O
sequence	O
of	O
finite-precision	O
floating-point	B-Algorithm
numbers	I-Algorithm
,	O
compared	O
to	O
the	O
obvious	O
approach	O
.	O
</s>
<s>
This	O
is	O
done	O
by	O
keeping	O
a	O
separate	O
running	O
compensation	O
(	O
a	O
variable	O
to	O
accumulate	O
small	O
errors	O
)	O
,	O
in	O
effect	O
extending	O
the	O
precision	B-Architecture
of	O
the	O
sum	O
by	O
the	O
precision	B-Architecture
of	O
the	O
compensation	O
variable	O
.	O
</s>
<s>
In	O
particular	O
,	O
simply	O
summing	O
numbers	O
in	O
sequence	O
has	O
a	O
worst-case	O
error	O
that	O
grows	O
proportional	O
to	O
,	O
and	O
a	O
root	B-General_Concept
mean	I-General_Concept
square	I-General_Concept
error	O
that	O
grows	O
as	O
for	O
random	O
inputs	O
(	O
the	O
roundoff	O
errors	O
form	O
a	O
random	O
walk	O
)	O
.	O
</s>
<s>
With	O
compensated	B-Algorithm
summation	I-Algorithm
,	O
using	O
a	O
compensation	O
variable	O
with	O
sufficiently	O
high	O
precision	B-Architecture
the	O
worst-case	O
error	B-Algorithm
bound	I-Algorithm
is	O
effectively	O
independent	O
of	O
,	O
so	O
a	O
large	O
number	O
of	O
values	O
can	O
be	O
summed	O
with	O
an	O
error	O
that	O
only	O
depends	O
on	O
the	O
floating-point	B-Algorithm
precision	B-Architecture
of	O
the	O
result	O
.	O
</s>
<s>
Similar	O
,	O
earlier	O
techniques	O
are	O
,	O
for	O
example	O
,	O
Bresenham	B-Algorithm
's	I-Algorithm
line	I-Algorithm
algorithm	I-Algorithm
,	O
keeping	O
track	O
of	O
the	O
accumulated	O
error	O
in	O
integer	O
operations	O
(	O
although	O
first	O
documented	O
around	O
the	O
same	O
time	O
)	O
and	O
the	O
delta-sigma	O
modulation	O
.	O
</s>
<s>
In	O
pseudocode	B-Language
,	O
the	O
algorithm	O
will	O
be	O
:	O
</s>
<s>
var	O
c	B-Language
=	O
0.0	O
//	O
A	O
running	O
compensation	O
for	O
lost	O
low-order	O
bits	O
.	O
</s>
<s>
var	O
y	O
=	O
input[i]	O
-	O
c	B-Language
//	O
c	B-Language
is	O
zero	O
the	O
first	O
time	O
around	O
.	O
</s>
<s>
sum	O
=	O
t	O
//	O
Algebraically	O
,	O
c	B-Language
should	O
always	O
be	O
zero	O
.	O
</s>
<s>
Beware	O
overly-aggressive	O
optimizing	B-Application
compilers	I-Application
!	O
</s>
<s>
This	O
algorithm	O
can	O
also	O
be	O
rewritten	O
to	O
use	O
the	O
Fast2Sum	B-Algorithm
algorithm	O
:	O
</s>
<s>
var	O
c	B-Language
=	O
0.0	O
//	O
A	O
running	O
compensation	O
for	O
lost	O
low-order	O
bits	O
.	O
</s>
<s>
var	O
y	O
=	O
input[i]	O
+	O
c	B-Language
//	O
c	B-Language
is	O
zero	O
the	O
first	O
time	O
around	O
.	O
</s>
<s>
(	O
sum	O
,	O
c	B-Language
)	O
=	O
Fast2Sum(sum,y )	O
//	O
sum	O
+	O
c	B-Language
is	O
an	O
approximation	O
to	O
the	O
exact	O
sum	O
.	O
</s>
<s>
Suppose	O
we	O
are	O
using	O
six-digit	O
decimal	O
floating-point	B-Algorithm
arithmetic	I-Algorithm
,	O
sum	O
has	O
attained	O
the	O
value	O
10000.0	O
,	O
and	O
the	O
next	O
two	O
values	O
of	O
input[i]	O
are	O
3.14159	O
and	O
2.71828	O
.	O
</s>
<s>
However	O
,	O
with	O
compensated	B-Algorithm
summation	I-Algorithm
,	O
we	O
get	O
the	O
correct	O
rounded	O
result	O
of	O
10005.9	O
.	O
</s>
<s>
Assume	O
that	O
c	B-Language
has	O
the	O
initial	O
value	O
zero	O
.	O
</s>
<s>
c	B-Language
=	O
(	O
10003.1	O
-	O
10000.0	O
)	O
-	O
3.14159	O
This	O
must	O
be	O
evaluated	O
as	O
written	O
!	O
</s>
<s>
But	O
on	O
the	O
next	O
step	O
,	O
c	B-Language
gives	O
the	O
error	O
.	O
</s>
<s>
c	B-Language
=	O
(	O
10005.9	O
-	O
10003.1	O
)	O
-	O
2.75987	O
This	O
extracts	O
whatever	O
went	O
in	O
.	O
</s>
<s>
So	O
the	O
summation	O
is	O
performed	O
with	O
two	O
accumulators	O
:	O
sum	O
holds	O
the	O
sum	O
,	O
and	O
c	B-Language
accumulates	O
the	O
parts	O
not	O
assimilated	O
into	O
sum	O
,	O
to	O
nudge	O
the	O
low-order	O
part	O
of	O
sum	O
the	O
next	O
time	O
around	O
.	O
</s>
<s>
Thus	O
the	O
summation	O
proceeds	O
with	O
"	O
guard	O
digits	O
"	O
in	O
c	B-Language
,	O
which	O
is	O
better	O
than	O
not	O
having	O
any	O
,	O
but	O
is	O
not	O
as	O
good	O
as	O
performing	O
the	O
calculations	O
with	O
double	O
the	O
precision	B-Architecture
of	O
the	O
input	O
.	O
</s>
<s>
However	O
,	O
simply	O
increasing	O
the	O
precision	B-Architecture
of	O
the	O
calculations	O
is	O
not	O
practical	O
in	O
general	O
;	O
if	O
input	O
is	O
already	O
in	O
double	O
precision	B-Architecture
,	O
few	O
systems	O
supply	O
quadruple	O
precision	B-Architecture
,	O
and	O
if	O
they	O
did	O
,	O
input	O
could	O
then	O
be	O
in	O
quadruple	O
precision	B-Architecture
.	O
</s>
<s>
A	O
careful	O
analysis	O
of	O
the	O
errors	O
in	O
compensated	B-Algorithm
summation	I-Algorithm
is	O
needed	O
to	O
appreciate	O
its	O
accuracy	O
characteristics	O
.	O
</s>
<s>
While	O
it	O
is	O
more	O
accurate	O
than	O
naive	O
summation	O
,	O
it	O
can	O
still	O
give	O
large	O
relative	B-Algorithm
errors	I-Algorithm
for	O
ill-conditioned	B-Algorithm
sums	O
.	O
</s>
<s>
(	O
computed	O
with	O
infinite	B-Algorithm
precision	I-Algorithm
)	O
.	O
</s>
<s>
where	O
is	O
the	O
machine	B-Algorithm
precision	I-Algorithm
of	O
the	O
arithmetic	O
being	O
employed	O
(	O
e.g.	O
</s>
<s>
for	O
IEEE	O
standard	O
double-precision	O
floating	B-Algorithm
point	I-Algorithm
)	O
.	O
</s>
<s>
In	O
the	O
expression	O
for	O
the	O
relative	B-Algorithm
error	I-Algorithm
bound	O
,	O
the	O
fraction	O
is	O
the	O
condition	B-Algorithm
number	I-Algorithm
of	O
the	O
summation	O
problem	O
.	O
</s>
<s>
Essentially	O
,	O
the	O
condition	B-Algorithm
number	I-Algorithm
represents	O
the	O
intrinsic	O
sensitivity	O
of	O
the	O
summation	O
problem	O
to	O
errors	O
,	O
regardless	O
of	O
how	O
it	O
is	O
computed	O
.	O
</s>
<s>
The	O
relative	B-Algorithm
error	I-Algorithm
bound	O
of	O
every	O
(	O
backwards	B-Algorithm
stable	I-Algorithm
)	O
summation	O
method	O
by	O
a	O
fixed	O
algorithm	O
in	O
fixed	O
precision	B-Architecture
(	O
i.e.	O
</s>
<s>
not	O
those	O
that	O
use	O
arbitrary-precision	B-Algorithm
arithmetic	I-Algorithm
,	O
nor	O
algorithms	O
whose	O
memory	O
and	O
time	O
requirements	O
change	O
based	O
on	O
the	O
data	O
)	O
,	O
is	O
proportional	O
to	O
this	O
condition	B-Algorithm
number	I-Algorithm
.	O
</s>
<s>
An	O
ill-conditioned	B-Algorithm
summation	O
problem	O
is	O
one	O
in	O
which	O
this	O
ratio	O
is	O
large	O
,	O
and	O
in	O
this	O
case	O
even	O
compensated	B-Algorithm
summation	I-Algorithm
can	O
have	O
a	O
large	O
relative	B-Algorithm
error	I-Algorithm
.	O
</s>
<s>
For	O
example	O
,	O
if	O
the	O
summands	O
are	O
uncorrelated	O
random	O
numbers	O
with	O
zero	O
mean	O
,	O
the	O
sum	O
is	O
a	O
random	O
walk	O
,	O
and	O
the	O
condition	B-Algorithm
number	I-Algorithm
will	O
grow	O
proportional	O
to	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
for	O
random	O
inputs	O
with	O
nonzero	O
mean	O
the	O
condition	B-Algorithm
number	I-Algorithm
asymptotes	O
to	O
a	O
finite	O
constant	O
as	O
.	O
</s>
<s>
If	O
the	O
inputs	O
are	O
all	O
non-negative	O
,	O
then	O
the	O
condition	B-Algorithm
number	I-Algorithm
is	O
1	O
.	O
</s>
<s>
Given	O
a	O
condition	B-Algorithm
number	I-Algorithm
,	O
the	O
relative	B-Algorithm
error	I-Algorithm
of	O
compensated	B-Algorithm
summation	I-Algorithm
is	O
effectively	O
independent	O
of	O
.	O
</s>
<s>
In	O
principle	O
,	O
there	O
is	O
the	O
that	O
grows	O
linearly	O
with	O
,	O
but	O
in	O
practice	O
this	O
term	O
is	O
effectively	O
zero	O
:	O
since	O
the	O
final	O
result	O
is	O
rounded	O
to	O
a	O
precision	B-Architecture
,	O
the	O
term	O
rounds	O
to	O
zero	O
,	O
unless	O
is	O
roughly	O
or	O
larger	O
.	O
</s>
<s>
In	O
double	O
precision	B-Architecture
,	O
this	O
corresponds	O
to	O
an	O
of	O
roughly	O
,	O
much	O
larger	O
than	O
most	O
sums	O
.	O
</s>
<s>
So	O
,	O
for	O
a	O
fixed	O
condition	B-Algorithm
number	I-Algorithm
,	O
the	O
errors	O
of	O
compensated	B-Algorithm
summation	I-Algorithm
are	O
effectively	O
,	O
independent	O
of	O
.	O
</s>
<s>
In	O
comparison	O
,	O
the	O
relative	B-Algorithm
error	I-Algorithm
bound	O
for	O
naive	O
summation	O
(	O
simply	O
adding	O
the	O
numbers	O
in	O
sequence	O
,	O
rounding	O
at	O
each	O
step	O
)	O
grows	O
as	O
multiplied	O
by	O
the	O
condition	B-Algorithm
number	I-Algorithm
.	O
</s>
<s>
In	O
practice	O
,	O
it	O
is	O
much	O
more	O
likely	O
that	O
the	O
rounding	O
errors	O
have	O
a	O
random	O
sign	O
,	O
with	O
zero	O
mean	O
,	O
so	O
that	O
they	O
form	O
a	O
random	O
walk	O
;	O
in	O
this	O
case	O
,	O
naive	O
summation	O
has	O
a	O
root	B-General_Concept
mean	I-General_Concept
square	I-General_Concept
relative	B-Algorithm
error	I-Algorithm
that	O
grows	O
as	O
multiplied	O
by	O
the	O
condition	B-Algorithm
number	I-Algorithm
.	O
</s>
<s>
This	O
is	O
still	O
much	O
worse	O
than	O
compensated	B-Algorithm
summation	I-Algorithm
,	O
however	O
.	O
</s>
<s>
However	O
,	O
if	O
the	O
sum	O
can	O
be	O
performed	O
in	O
twice	O
the	O
precision	B-Architecture
,	O
then	O
is	O
replaced	O
by	O
,	O
and	O
naive	O
summation	O
has	O
a	O
worst-case	O
error	O
comparable	O
to	O
the	O
term	O
in	O
compensated	B-Algorithm
summation	I-Algorithm
at	O
the	O
original	O
precision	B-Architecture
.	O
</s>
<s>
In	O
practice	O
,	O
it	O
is	O
more	O
likely	O
that	O
the	O
errors	O
have	O
random	O
sign	O
,	O
in	O
which	O
case	O
terms	O
in	O
are	O
replaced	O
by	O
a	O
random	O
walk	O
,	O
in	O
which	O
case	O
,	O
even	O
for	O
random	O
inputs	O
with	O
zero	O
mean	O
,	O
the	O
error	O
grows	O
only	O
as	O
(	O
ignoring	O
the	O
term	O
)	O
,	O
the	O
same	O
rate	O
the	O
sum	O
grows	O
,	O
canceling	O
the	O
factors	O
when	O
the	O
relative	B-Algorithm
error	I-Algorithm
is	O
computed	O
.	O
</s>
<s>
So	O
,	O
even	O
for	O
asymptotically	O
ill-conditioned	B-Algorithm
sums	O
,	O
the	O
relative	B-Algorithm
error	I-Algorithm
for	O
compensated	B-Algorithm
summation	I-Algorithm
can	O
often	O
be	O
much	O
smaller	O
than	O
a	O
worst-case	O
analysis	O
might	O
suggest	O
.	O
</s>
<s>
In	O
pseudocode	B-Language
,	O
the	O
algorithm	O
is	O
:	O
</s>
<s>
var	O
c	B-Language
=	O
0.0	O
//	O
A	O
running	O
compensation	O
for	O
lost	O
low-order	O
bits	O
.	O
</s>
<s>
c	B-Language
+	O
=	O
(	O
sum	O
-	O
t	O
)	O
+	O
input[i]	O
//	O
If	O
sum	O
is	O
bigger	O
,	O
low-order	O
digits	O
of	O
input[i]	O
are	O
lost	O
.	O
</s>
<s>
c	B-Language
+	O
=	O
(	O
input[i]	O
-	O
t	O
)	O
+	O
sum	O
//	O
Else	O
low-order	O
digits	O
of	O
sum	O
are	O
lost	O
.	O
</s>
<s>
return	O
sum	O
+	O
c	B-Language
//	O
Correction	O
only	O
applied	O
once	O
in	O
the	O
very	O
end	O
.	O
</s>
<s>
This	O
enhancement	O
is	O
similar	O
to	O
the	O
replacement	O
of	O
Fast2Sum	B-Algorithm
by	O
2Sum	B-Algorithm
in	O
Kahan	O
's	O
algorithm	O
rewritten	O
with	O
Fast2Sum	B-Algorithm
.	O
</s>
<s>
For	O
summing	O
in	O
double	O
precision	B-Architecture
,	O
Kahan	O
's	O
algorithm	O
yields	O
0.0	O
,	O
whereas	O
Neumaier	O
's	O
algorithm	O
yields	O
the	O
correct	O
value	O
2.0	O
.	O
</s>
<s>
In	O
pseudocode	B-Language
,	O
the	O
algorithm	O
is	O
:	O
</s>
<s>
Although	O
Kahan	O
's	O
algorithm	O
achieves	O
error	O
growth	O
for	O
summing	O
n	O
numbers	O
,	O
only	O
slightly	O
worse	O
growth	O
can	O
be	O
achieved	O
by	O
pairwise	B-Algorithm
summation	I-Algorithm
:	O
one	O
recursively	O
divides	O
the	O
set	O
of	O
numbers	O
into	O
two	O
halves	O
,	O
sums	O
each	O
half	O
,	O
and	O
then	O
adds	O
the	O
two	O
sums	O
.	O
</s>
<s>
The	O
equivalent	O
of	O
pairwise	B-Algorithm
summation	I-Algorithm
is	O
used	O
in	O
many	O
fast	O
Fourier	O
transform	O
(	O
FFT	O
)	O
algorithms	O
and	O
is	O
responsible	O
for	O
the	O
logarithmic	O
growth	O
of	O
roundoff	O
errors	O
in	O
those	O
FFTs	O
.	O
</s>
<s>
In	O
practice	O
,	O
with	O
roundoff	O
errors	O
of	O
random	O
signs	O
,	O
the	O
root	B-General_Concept
mean	I-General_Concept
square	I-General_Concept
errors	O
of	O
pairwise	B-Algorithm
summation	I-Algorithm
actually	O
grow	O
as	O
.	O
</s>
<s>
Another	O
alternative	O
is	O
to	O
use	O
arbitrary-precision	B-Algorithm
arithmetic	I-Algorithm
,	O
which	O
in	O
principle	O
need	O
no	O
rounding	O
at	O
all	O
with	O
a	O
cost	O
of	O
much	O
greater	O
computational	O
effort	O
.	O
</s>
<s>
A	O
way	O
of	O
performing	O
exactly	O
rounded	O
sums	O
using	O
arbitrary	B-Algorithm
precision	I-Algorithm
is	O
to	O
extend	O
adaptively	O
using	O
multiple	O
floating-point	B-Algorithm
components	O
.	O
</s>
<s>
This	O
will	O
minimize	O
computational	O
cost	O
in	O
common	O
cases	O
where	O
high	O
precision	B-Architecture
is	O
not	O
needed	O
.	O
</s>
<s>
c	B-Language
=	O
(	O
t	O
-	O
sum	O
)	O
-	O
y	O
;	O
</s>
<s>
c	B-Language
=	O
( ( 	O
sum	O
+	O
y	O
)	O
-	O
sum	O
)	O
-	O
y	O
;	O
</s>
<s>
c	B-Language
=	O
0	O
;	O
</s>
<s>
In	O
practice	O
,	O
many	O
compilers	O
do	O
not	O
use	O
associativity	O
rules	O
(	O
which	O
are	O
only	O
approximate	O
in	O
floating-point	B-Algorithm
arithmetic	I-Algorithm
)	O
in	O
simplifications	O
,	O
unless	O
explicitly	O
directed	O
to	O
do	O
so	O
by	O
compiler	O
options	O
enabling	O
"	O
unsafe	O
"	O
optimizations	O
,	O
although	O
the	O
Intel	B-Language
C++	I-Language
Compiler	I-Language
is	O
one	O
example	O
that	O
allows	O
associativity-based	O
transformations	O
by	O
default	O
.	O
</s>
<s>
The	O
original	O
K&R	O
C	B-Language
version	O
of	O
the	O
C	B-Language
programming	I-Language
language	I-Language
allowed	O
the	O
compiler	O
to	O
re-order	O
floating-point	B-Algorithm
expressions	O
according	O
to	O
real-arithmetic	O
associativity	O
rules	O
,	O
but	O
the	O
subsequent	O
ANSI	O
C	B-Language
standard	O
prohibited	O
re-ordering	O
in	O
order	O
to	O
make	O
C	B-Language
better	O
suited	O
for	O
numerical	O
applications	O
(	O
and	O
more	O
similar	O
to	O
Fortran	B-Application
,	O
which	O
also	O
prohibits	O
re-ordering	O
)	O
,	O
although	O
in	O
practice	O
compiler	O
options	O
can	O
re-enable	O
re-ordering	O
,	O
as	O
mentioned	O
above	O
.	O
</s>
<s>
A	O
portable	O
way	O
to	O
inhibit	O
such	O
optimizations	O
locally	O
is	O
to	O
break	O
one	O
of	O
the	O
lines	O
in	O
the	O
original	O
formulation	O
into	O
two	O
statements	O
,	O
and	O
make	O
two	O
of	O
the	O
intermediate	O
products	O
volatile	B-Operating_System
:	O
</s>
<s>
In	O
general	O
,	O
built-in	O
"	O
sum	O
"	O
functions	O
in	O
computer	O
languages	O
typically	O
provide	O
no	O
guarantees	O
that	O
a	O
particular	O
summation	O
algorithm	O
will	O
be	O
employed	O
,	O
much	O
less	O
Kahan	B-Algorithm
summation	I-Algorithm
.	O
</s>
<s>
The	O
BLAS	B-Application
standard	O
for	O
linear	B-Language
algebra	I-Language
subroutines	O
explicitly	O
avoids	O
mandating	O
any	O
particular	O
computational	O
order	O
of	O
operations	O
for	O
performance	O
reasons	O
,	O
and	O
BLAS	B-Application
implementations	O
typically	O
do	O
not	O
use	O
Kahan	B-Algorithm
summation	I-Algorithm
.	O
</s>
<s>
The	O
standard	O
library	O
of	O
the	O
Python	B-Language
computer	I-Language
language	I-Language
specifies	O
an	O
function	O
for	O
exactly	O
rounded	O
summation	O
,	O
using	O
the	O
Shewchuk	O
algorithm	O
to	O
track	O
multiple	O
partial	O
sums	O
.	O
</s>
<s>
In	O
the	O
Julia	B-Application
language	I-Application
,	O
the	O
default	O
implementation	O
of	O
the	O
sum	O
function	O
does	O
pairwise	B-Algorithm
summation	I-Algorithm
for	O
high	O
accuracy	O
with	O
good	O
performance	O
,	O
but	O
an	O
external	O
library	O
provides	O
an	O
implementation	O
of	O
Neumaier	O
's	O
variant	O
named	O
sum_kbn	O
for	O
the	O
cases	O
when	O
higher	O
accuracy	O
is	O
needed	O
.	O
</s>
<s>
In	O
the	O
C#	B-Application
language	O
,	O
implements	O
the	O
Neumaier	O
variant	O
and	O
pairwise	B-Algorithm
summation	I-Algorithm
:	O
both	O
as	O
scalar	O
,	O
data-parallel	O
using	O
SIMD	B-Device
processor	O
instructions	O
,	O
and	O
parallel	O
multi-core	O
.	O
</s>
