asaguiar wrote:

> Given the pseudocode explanation of the Kahan algorithm at

> http://en.wikipedia.org/wiki/Kahan_summation_algorithm, I tried to

> implement it in C. It is supposed to minimize the effect of base

> conversion errors after repeated sum operations.
No, it minimizes summation roundoff or truncation errors due to unequal

scale factors, not conversion errors. There is no base conversion being

done in the summation loop.

> However, the effect is null. The rounding error persists without

> change.

> My implementation is:

>

> double kahanSum(double input, double tosum, double times)

> {

> double c=0.0, sum=input,y,t;

> int count;

> for(count=0; count<times; count++)

> {

> y=tosum-c;

> t=sum+y;

> c=(t-sum)-y;

> sum=t;

> }

> return(sum);

> }

>

> Could you, please, point where I went wrong?
That seems correct to me, although it is a little strange to use a

floating point value for the counter. Here is a full implementation of

a test program. This demonstrates, on my system, a definite retention

of precision in the summation.

/* Test of kahanSum */

#include <stdio.h>

double kahanSum(double input, double tosum, double times)

{

double c=0.0, sum=input,y,t;

int count;

for(count=0; count<times; count++)

{

y=tosum-c;

t=sum+y;

c=(t-sum)-y;

sum=t;

}

return(sum);

}

#define PI 3.1415926535897932384626

int main(void) {

double s;

long int i;

s = 0.0;

for (i=0; i < 10000; i++) {

s += PI;

}

printf ("(double)pi = %.18g\n", PI);

printf ("10000 additions of pi = %.18g, error = %.6g\n",

s, 10000.*PI-s);

printf ("10000 Kahan additions of pi = %.18g, error = %.6g\n",

kahanSum(0.0,PI, 10000), 10000.*PI-kahanSum(0.0,PI, 10000));

return 0;

}

--

Thad