double x[ N]; // given data array, N ≥ 2 double mean = sum( x) / N; double varn = sum( square( x - mean)) / (N - 1);The second pass is often a nuisance, especially when the data comes from a file or some function. A bit of algebra obscures the meaning, but gives a one-pass formula:
double sumXi = sum( x); // compute both sums in one loop double sumSq = sum( square(x)); double varn = (sumSq - square( sumXi)/N) / (N - 1);Of course, you recall that standard deviation is just the square root of the variance.
| D ≠ 0 | // if D = 0, any value of Q can satisfy the definition |
| abs(R) < abs(D) | // R is smaller than D, in magnitude |
| N * R > 0 | // R has same sign as N |
| Q * (N * D) ≥ 0 | // Q has same sign as N * D |
In other words, when negative values are involved this modulus is not the same as a mathematical modulus, which is never negative.
Fore-warned is fore-armed.