Standard Deviation

Stephen M. Dunn cs3b3aj at maccs.McMaster.CA
Fri Mar 24 02:20:52 AEST 1989


   Well, of course, the standard textbook methods of calculating the
standard deviation are as follows:

1)  assuming values are in array x, there are n values, and the mean is 
    known and is in the variable mean: 

temp = 0;
for (i = 1; i <= n; i++)
   temp += (x [i] - mean) ^ 2;
std_dev = sqrt (temp / n);

    (note that my C is _so_ rusty, I'm not even sure if I did the
     exponentiation correctly!  Anyway, the algorithm is right.) 

2)  assuming that the SQUARES of the values are in array x, there are n 
    values, and the mean is known and is in the variable mean: 

temp = 0;
for (i = 1; i <= n; i++)
   temp += x [i] ^ 2;
std_dev = sqrt ((temp - (mean ^ 2) / n) / n);

   In both cases, temp, x and mean would likely be doubles, and n and i 
would be ints. 
   Depending on the magnitudes of your numbers, one of these algorithms may 
be more accurate than the other.
   Hope these help.

Regards,

-- 
======================================================================
! Stephen M. Dunn, cs3b3aj at maccs.McMaster.CA ! DISCLAIMER:           !
! I always wanted to be a lumberjack! - M.P. ! I'm only an undergrad !
======================================================================



More information about the Comp.lang.c mailing list