0

I am writing some statistical functions in C++, specifically the corrected sample standard deviation function. I also write test suites with assertions to make sure that my code works as expected. I use online calculator to pre-determine the correct values for my test suites and check it against the output values from my stdev function using the assert keyword. I also printed out the values of my function output and they are identical to what I set them to be within the assert statement. The asserts should all be true since I am basically running assert(c == c) but it always fails. I am giving my test suites here as well as my stdev function.

void test_stdev_function() {
  vector<double> v1, v2, v3, v4;
  v1 = {0, 1, 2};
  v2 = {1, 1, 1};
  v3 = {-2, -1, 1, 2};
  v4 = {1, 1, 1, 10};
  assert(stdev(v1) == 1);
  assert(stdev(v2) == 0);
  assert(stdev(v3) == 1.82574);
  assert(stdev(v4) == 4.5);
  cout << "function stdev pass!" << endl;
}

double stdev(std::vector<double> v)
{
    double sum_square_diff = 0;
    double avg = 0;
    double sum = 0;
    int n = v.size();
    for (std::vector<double>::iterator itr = v.begin(); itr != v.end(); ++itr) {
        sum+=*itr;
        cout << "sum = " << sum << "  *itr = " << *itr << endl;
    }
    avg = sum / n;
    cout << "avg = " << avg << "    ";
    for (std::vector<double>::iterator itr = v.begin(); itr != v.end(); itr++) {
        sum_square_diff+=pow((*itr - avg), 2);
    }
    double stdev = sqrt(sum_square_diff / (n-1));
    cout << "stdev = " << stdev << endl;
    return stdev;
}

Anyone knows why the assert statements keep failing?

Johnny Chan
  • 77
  • 1
  • 8
  • Don't understand why the down votes..if you think this is a dumb question fine. At least help me if you can or if I am not specific enough let me know I'll try to be more detailed. I've done quite some research and debugging and still couldn't figure this out so this is my last hope here, seriously. – Johnny Chan Nov 20 '16 at 06:02
  • did you just guess assert(c == c)? – Lorence Hernandez Nov 20 '16 at 06:05
  • I don't quite understand what you mean. I used assert(c==c) as an analogy, I doubled checked the values of each stdev values and they match exactly with the numerical values in my assert argument, hence I have, in the first assert statement, assert(1 == 1) which is perfectly fine but the third and fourth asserts failed. – Johnny Chan Nov 20 '16 at 06:10
  • i mean if you just guess that they are always true. sec im going to try test your code – Lorence Hernandez Nov 20 '16 at 06:12
  • 2
    Related: http://stackoverflow.com/questions/588004/is-floating-point-math-broken –  Nov 20 '16 at 06:14
  • @JohnnyChan: While I did not downvote myself, I think this question could be improved by explaining exactly *which* of the 4 assertions fail and *why* they evaluate to false, i.e. what is the actual result in each of the four lines. You've put a `cout` at the end of the function but did not add the output to your question, which makes answering harder. – Christian Hackl Nov 20 '16 at 10:59

2 Answers2

2

Something like this could account for floating point uncertainty:

assert((fabs(stdev(v3) - 1.82574) < 0.00001));
1

The first two asserts work because the stdev return values are whole numbers. The third one fails because there are a different number of decimal places returned from stdev than what you have written. The comparison fails and assert calls abort().

kaineub
  • 162
  • 10