I have several functions that return floating point values. I am currently exploring unit test suites and would like to write up some pretty extensive tests. My question is - what's the best way to test floating point values? A difficulty with floating point numbers is that if I perform the same calculation in a different order I may get a slightly different number due to shifting and roundoff.
Quite some time ago I used a testing framework for Visual Basic that had an assertion function that took two floating point values and an integer value for the number of places of precision beyond the decimal to compare. I am playing around with check right now and I don't think they have a similar function.
I suppose I can do something similar by going through the calculations on my own and then doing something like:
Is that an alright way to test floating point values? Is there a preferred/standard way of doing this?Code:float answer; float retval; answer = ... /* Some long computation here */ retval = function_to_test(some, args); /* Supposedly, same computation here */ /* Test to some (arbitrary?) level of precision */ /* fail_unless() is provided by Check...it is just the test/comparison function */ fail_unless(fabs(answer - retval) < 0.0001, "failing message");