1

I'm trying to implement boundary tests in CppUnit. I want to check the limit value itself as well as the boundaries around this limit.

For the upper boundary I wanted to add the smallest inkrement possible. For double this increment can be accessed with

numeric_limits<double>::epsilon()

However, if I add epsilon to my limit I get Not-a-Number (NaN) as result:

#include <stdio.h>
#include <iostream>
#include <limits>
#include <math.h>

using namespace std;

const double WARNING_LIMIT = 8000.0;

int main(void) {

double warningLowerLimit = WARNING_LIMIT - numeric_limits<double>::epsilon();

if(warningLowerLimit < WARNING_LIMIT ) {
    cout << "lower" << endl;
}
else if (warningLowerLimit > WARNING_LIMIT) {
    cout << "upper" << endl;
}
else if ( fabs(warningLowerLimit) < 0.001) {
    cout << "same" << endl;
}
else {
    cout << "NaN" << endl; // <-- result
}

}

Can somebody please explain me, why the result is not lower that the limit?

Best regards

tobiger
  • 103
  • 1
  • 12

1 Answers1

0
else if ( fabs(warningLowerLimit) < 0.001) {
    cout << "same" << endl;
}

That should be

fabs(warningLowerLimit - WARNING_LIMIT)

there. Without checking the difference, you get to the cout << "NaN" if warningLowerLimit == WARNING_LIMIT for example.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431