I am using Catch v2.13.1
What is the correct way to compare float values. I thought the below would fail, but both pass.
REQUIRE(1147332687.7189338 == Approx(1147332688.4281545).margin(0.0001));
REQUIRE(1147332687.7189338 == Approx(1147332688.4281545));
However this fails as expected
REQUIRE(abs(1147332687.7189338 - 1147332688.4281545) <= Approx(0).margin(0.0001));
I don't understand why the first two statements wouldn't work
There are a couple of things to consider in the posted example.
This will pass, "unexpectedly". The reason can be found in the documentation (assertions - floating point comparisons).
In the posted example, the two numbers differ by a cofficient near 6.2e-10, while the default is (given a 32-bit float) near 1.2e-5.
The following test wouldn't pass.
The other tests involve
margin
, which is described in the documentation asThe caveat, though, can be found in issue#1507.
So, this test wouldn't pass:
Note that this "issue" seems to be marked as resolved - not a bug: