Yes, floats and a large array. At first glance I was thinking maybe this was done on purpose to test the compiler but I guess not.

It is however *technically correct* if the values are within limits, so if you use a smaller array size or doubles the result should be the same.

`double KahanSum(const double *data, int n)`

{

double sum = 0.0f, C = 0.0f, Y, T;

for (int i = 0; i < n; i++)

{

Y = *data++ - C;

T = sum + Y;

C = T - sum - Y;

sum = T;

}

return sum;

}

double KahanSum2(const double *data, int n)

{

double sum = 0.0f;

for (int i = 0; i < n; i++)

{

sum += *data++;

}

return sum;

}

int main(int c, char **a)

{

int count = 1000000;

double *source = malloc(count * sizeof(double));

for (int i = 0; i < count; ++i)

source[i] = (double)(rand()) / (double)(RAND_MAX);

LARGE_INTEGER start, mid, end;

double sum1 = 0.0f, sum2 = 0.0f;

QueryPerformanceCounter(&start);

sum1 = KahanSum(source, count);

QueryPerformanceCounter(&mid);

sum2 = KahanSum2(source, count);

QueryPerformanceCounter(&end);

printf(" sum1: %.1f in %u\n", sum1, (mid.QuadPart - start.QuadPart));

printf(" sum2: %.1f in %u\n", sum2, (end.QuadPart - mid.QuadPart));

return 0;

}

sum1: 499538.5 in 44493

sum2: 499538.5 in 8287