catchorg/Catch2

WithinRel and numbers approaching 0.0

sudara opened this issue · 1 comments

Describe the bug

I love Catch2 and use it daily!

The only thing I regularly wrestle with is the usability around small numbers around zero.

For example, I just had this matcher fail:

            REQUIRE_THAT (result, Catch::Matchers::Approx<float> ({ 0.0, 1.0f, -0.0f, -1.0 }));

with this (unhelpful) expansion:

with expansion:
  { 0.0f, 1.0f, -0.0f, 0.0f } is approx: { 0.0f, 1.0f, -0.0f, -1.0f }

I wasn't sure which number was the problem, nor how much it's off by. Is it possible to add a margin here? Or to output the full representation of the number?

On additional manual inspection, negative 0 was actually -0.0000000874228f.

So then I wrote

            float value = -0.0000000874228f;
            REQUIRE_THAT (value, Catch::Matchers::WithinRel (0.0f, 0.001f));

which fails with

  -0.0f and 0 are within 0.1% of each other

which is incorrect.

I moved the decimal and wrote

            REQUIRE_THAT (value, Catch::Matchers::WithinRel (0.0f, 0.1f));

Which fails with

  -0.0f and 0 are within 10% of each other

Maybe I'm confused about WithinRel usage?

It seems that WithinAbs is nice and happy, I'll stick with that for now. Not sure how to make the vector version happy...

Platform information:

  • Catch version: v3.3.2
  • Matchers::Approx uses Approx, not the floating point matchers. Sadly the string output uses limited precision, so the output is not great.
  • I don't understand your issue with WithinRel. The behaviour is as it should be, 0.0000000874228f and 0.0f are not in any reasonable relative relation with each other (you would need to set coefficient to 1., or 100%, for them to be related). Is it about the output? It goes something like
/mnt/c/ubuntu/Catch2/tests/SelfTest/UsageTests/Matchers.tests.cpp:526:FAILED:
  REQUIRE_THAT( 10., WithinRel( 12.1, 0.1 ) )
with expansion:
  10.0 and 12.1 are within 10% of each other

Which means that the check "10.0 and 12.1 are within 10% of each other" has failed.