we have been struggling for a while with one of the unit test. During the study, we found the main reason, which, apparently, is a comparison in floats (see the following code fragment, where I simplified the calculation, but it still does not work).
TEST_F( MyFloatTest, thisOneDoesFail) { const float toCompare = 0.2f - 1.0f + 0.9f; EXPECT_FLOAT_EQ( toCompare, 0.1f ); }
Result:
Actual: 0.1 Expected: toCompare Which: 0.099999964
With some experience in numerical mathematics, we still cannot understand why this test fails, while a custom floating point comparison using std :: numeric_limits :: epsilon has passed. Therefore, at some point, we began to think that GTest was wrong, and we are debugging it. He uses strange expressions that we are not completely capturing. What's else weird: the following test passes, although I just add 1:
TEST_F( MyFloatTest, thisOnePasses) { const float toCompare = 1.2f - 1.0f + 0.9f; EXPECT_FLOAT_EQ( toCompare, 1.1f ); }
We thought this might be a problem when negative float values ββare included, but the following test also passes:
TEST_F( MyFloatTest, thisOnePassesAlso) { const float toCompare = 0.2f - 1.0f + 1.9f; EXPECT_FLOAT_EQ( toCompare, 1.1f ); }
So, it seems to us that the macro EXPECT_FLOAT_EQ from Gtest just has a problem around zero. Does anyone know about this behavior? Have you ever seen this in your midst? (By the way, we are using MSVC2015). Is it just by chance, due to the accuracy of the 4 ULPs mentioned in GTest? (which is also not entirely clear to us).