2008/07/23

A better average


double variance(long long *x, int n)
{
double m = x[0];
double s = 0;
int i;
for (i=0; i double m_new = m + (x[i] - m)/(i+1);
s = s + (x[i] - m)*(x[i] - m_new);
m = m_new;
}
return s / (n - 1);
}

double mean(long long *x, int n)
{
double m = x[0];
int i;
for (i=0; i m = m + (x[i] - m)/(i+1);
}
return m;
}

double stddev(long long *x, int n)
{
return sqrt(variance(x, n));
}

long long times[30];

printf("mean: %f stddev: %f\n", mean(times, 20), stddev(times, 20));

0 件のコメント: