So I'm using CUnit to do unit testing. I expect something like
float x;
x = atof("17.99");
And I want to test this with an assert; obviously with some small epsilon I could
CU_ASSERT(abs(x - atof("17.99")) < epsilon);
However I'm considering
r = atof("17.99");
CU_ASSERT(0 == memcmp(&x, &r, sizeof(float));
This does appear to work. I wish to use this to avoid having to manually set epsilon on each test based on the values. In the above case 1e-6 should be sufficient; however if the value is 1e-10 using epsilon of 1e-6 may not catch a problem. The more choices the developer has to make the more room for error.
My questions are: Should this technique be stable on posix systems? That is, if the two floating point numbers being compared are generated by exactly the same steps should their internal representation be exactly the same.
edit: More to the point I'd eventually like a CU_ASSERT_FLOAT_EQUAL macro.