Thread: Standard Deviation in C++

  1. #1
    Unregistered
    Guest

    Angry 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. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    How big are your numbers?

    Doubles are plenty big on 32 bit compilers, I doubt you are exceeding their maximum.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest

    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. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    2
    If you are using long doubles, they should be plenty in size unless you are really scrambling for sig figs.

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to calculate standard deviation
    By hobilla in forum C Programming
    Replies: 13
    Last Post: 03-14-2009, 06:41 AM
  2. help with standard deviation
    By belkins in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 11:04 PM
  3. Loop and standard deviation problem
    By Kyeong in forum C Programming
    Replies: 8
    Last Post: 05-24-2008, 03:20 AM
  4. Standard Deviation of an array of numbers...
    By Xenofizz in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2003, 10:48 PM
  5. arrays and standard deviation
    By bruceness in forum C Programming
    Replies: 1
    Last Post: 10-28-2002, 09:35 PM