According to Google Test's documentation about floating point comparison, EXPECT_DOUBLE_EQ
and ASSERT_DOUBLE_EQ
verify that the two double values val1
and val2
are approximately equal, to within 4 ULPs (Units in the Last Place) from each other.
The following code example, however, fails the test even though the difference in the two doubles occur in 2 ULPs.
TEST(Foo, doubles_match)
{
ASSERT_DOUBLE_EQ(0.62469504755442462, 0.62469504755442407);
}
Results:
error: Expected equality of these values:
0.62469504755442462
0.62469504755442407
What am I missing?
The numbers mentioned in the question are 5 ULPs apart (considering binary 64-bit IEEE-754 numbers). You can verify this with this program:
#include <bit>
#include <cstdint>
#include <print>
int main() {
const std::uint64_t a = std::bit_cast<std::uint64_t>(0.62469504755442462);
const std::uint64_t b = std::bit_cast<std::uint64_t>(0.62469504755442407);
std::print("{:016x}\n", a);
std::print("{:016x}\n", b);
std::print("diff: {}\n", a - b);
}
It outputs:
3fe3fd8077e7057a
3fe3fd8077e70575
diff: 5