Short answer: better summing method is Kahan summation algorithm. This answer corrected stated that
" It has the same algorithmic complexity as a naive summation ; It will greatly increase the accuracy of a summation. ", and also gave an implementation in C++.
Kahan summation is only necessary if the elements of your array vastly differ in magnitude or if you really need the 16 digits of precision that double in principle can offer (rare situation).
So, before coding kahan summation in C you should do some checks. Given the GSL implementation of gsl_stats_mean
is
(GSL 1.16 source code)
/* Compute the arithmetic mean of a dataset using the recurrence relation
mean_(n) = mean(n-1) + (data[n] - mean(n-1))/(n+1) */
long double mean = 0;
size_t i;
for (i = 0; i < size; i++)
{
mean += (data[i * stride] - mean) / (i + 1);
}
I cannot immediately see that this would avoid lost of precision if your numbers are indeed vastly different in magnitude (there is a direct sum between your highly variable numbers and the mean, which evolves slowly in magnitude.). A good check is to sort your array before calculating the sum/mean using your naive implementation/gsl.
Edit 1 : Warning, c = (t - sum) - y
may be optimized to c = 0
if optimization is turn on.