I've written a function that tests two floating point numbers for approximate equality (see this Code Review question). I'd like to unit test my function, but I'm not positive of the best way to do this. Obviously I could pick some arbitrary numbers that should be equal within the threshold, but it seems a lot more useful to me to test that actual computations that should be equal but fail a naive equality test (due to rounding errors) are considered equal by my function.
Is that valid, or should I just pick my magic numbers and move along? Are there standard test cases/examples that people have historically used? I tried to find something, but all I found was a bunch of references explaining why I shouldn't use exact equality in floating point unit tests, which I already know.
As an example, I could write a test like this (using gtest):
template <typename FP>
class FloatEquality {
protected:
FP left, right, diff;
std::size_t ulps;
virtual void SetUp()
{
left = 2.0;
right = 2.1;
diff = .2;
}
};
TYPED_TEST_CASE_P(FloatEquality);
TYPED_TEST_P(FloatEquality, MagicNumbers)
{
EXPECT_TRUE(nearlyEqual(this->left, this->right, this->diff, this->ulps));
}
REGISTER_TYPED_TEST_CASE_P(FloatEquality, MagicNumbers);
using FloatingPointTypes= ::testing::Types<float, double>;
INSTANTIATE_TYPED_TEST_CASE_P(FloatingPoint, FloatEquality, FloatingPointTypes);
These numbers are obviously not a great choice, but they exemplify the types of magic numbers I could choose here that would be able to check all of my boxes and give me good code coverage, but don't seem that meaningful.
I did end up finding one example of someone unit testing this, but that is the magic number approach. The numbers appear to be reasonably well chosen, but it still feels like we aren't quite testing the right thing.