tags:

views:

62

answers:

3
    double s_deviation(double data[],int cnt, double mean)
   {
      int i;
      double sum= 0;
      double sdeviation;
      double x;

    //x = mean(billy,a_size);

      for(i=0; i<cnt; i++)
      {    
         sum += ((data[i]) - (mean));
      } 

        sdeviation = sqrt(sum/((double)cnt));

      return sdeviation;
   }

When I cout the result from this function, it gave me NaN. I tested the value of (mean) and data[i] using return data[i] and return mean they are valid.

when i replaced mean with an actual number, the operation returned a finite number. but with mean as a variable, it produced NaH. I can't see anything wrong with my code at the moment.

Again, I am sure mean, data are getting the right number based on those tests.

Thank you

+4  A: 

I'd guess that the value of mean is large relative to your data, so that some of the ((data[i]) - (mean)) values are negative, and so overall sum ends up being negative.

Then, when you try to compute sqrt(sum/((double)cnt)), you are taking the square root of a negative number, which results in complex number, which is not representable by a double.

However, the underlying problem is that your standard deviation algorithm is incorrect. You are supposed to sum the squares of the distances from the mean, not the distances themselves. Aside from making your computation correct, this also guarantees that sum is never negative, and so you can always get a real-valued square root.

Tyler McHenry
yes you are correct. it was my fd's code, and i was tuoring him. hahaha thx, you notice that too... :) yeah
JohnWong
+1  A: 

I think You should have

for(i=0; i<cnt; i++)
{    
   sum += ((data[i]) - (mean)) * ((data[i]) - (mean));
} 

In the version You have now sum should be 0, but due to some rounding errors it's most probably a small negative value.

Maciej Hehl
thank you for your input
JohnWong
A: 

You're taking the sqrt of a negative number (most likely), and that's because you're using the wrong formula for standard deviation.

Standard dev is not the sqrt of the average of (val-mean), it's the sqrt of the average SQUARE of (val-mean).

Larry Gritz
yes. thank you for pointing that out :)
JohnWong