# Standard Deviation in C++

• 09-12-2001
Unregistered
Standard Deviation in C++
How do you perform standard deviation in c++, the addition of the difference between the mean and the current number gets to astronomical for what im using, no data type wrks HELP!!!!!!

Nate
• 09-12-2001
SilentStrike

Doubles are plenty big on 32 bit compilers, I doubt you are exceeding their maximum.
• 09-12-2001
Unregistered
Size...
Im taking the salaries of a baseball team which range from 200000 to 12000000, and have used doubles, long ints unsigned int, float unsigned float etc.
• 09-12-2001
lieten
If you are using long doubles, they should be plenty in size unless you are really scrambling for sig figs.
• 09-12-2001
Nick
Does this work?

Code:

```#include <cmath> using namespace std; #include <cassert> double sum_squares(double data[], int n) {       double sum = 0;       for (int i = 0; i < n; ++i)               sum += data[i] * data[i];       return sum; } double calc_std_deviation(double data[], int n) {       assert(n > 1);       double  ss = sum_squares(data, n);       return sqrt(ss / (n - 1));      }```
There is another way to calculate std deviation by doing
some algebra on the summation and then getting a similar
formula.
• 09-14-2001
Nick
Made a big mistake in that one as I forgot to
subtract x_i from the mean and then squaring that. On the division, when I divide by n-1 it is for the sample, dividing by n is for the population I think.