# Standard Deviation in C++

This is a discussion on Standard Deviation in C++ within the C++ Programming forums, part of the General Programming Boards category; How do you perform standard deviation in c++, the addition of the difference between the mean and the current number ...

1. ## 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

2. How big are your numbers?

Doubles are plenty big on 32 bit compilers, I doubt you are exceeding their maximum.

3. ## 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.

4. If you are using long doubles, they should be plenty in size unless you are really scrambling for sig figs.

5. 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.

6. 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.