#3197

Floating point printing should print shortest round-trip value

Openbrevzinopened 2mo ago

Description

Currently, Catch2 uses iostream with fixed precision for printing float and double. This is fine, until it's not. Consider comparing two doubles that are 1 ULP apart:

CHECK(5.000000000000001e-05 == 5e-05);

This fails with the amusingly unhelpful:

/app/example.cpp:4: FAILED:
  CHECK( 5.000000000000001e-05 == 5e-05 )
with expansion:
  0.00005 == 0.00005

I have to go through and change Catch::StringMaker<double>::precision to be at least 20 to see the difference, but really also exactly 20 to have sensible output. Here are different precision outputs, compared to the output we get with format (std::format / fmt::format / std::to_chars):

precisionoutput
190.00005 == 0.00005
200.00005000000000000001 == 0.00005
210.000050000000000000009 == 0.000050000000000000002
fmt5.000000000000001e-05 == 5e-05

The last line is just a lot better. I don't think there's even a set of options across iostreams/iomanip to give you this output.

Additional context

Out of the box std::to_chars would give this result. I don't know what the compiler requirements are for Catch2 to know whether that's a viable option. Otherwise, there's an increasing number of impressive floating point writing algorithms that you could just consume, e.g. zmij.

Activity

2
brevzin2mo ago

I guess if you did #2830, then I could do this on my own, which is fine for me. But I'm not sure I'm the only person who would benefit, and also I can just patch Catch2 on my own anyway.

ecorm1mo ago

This is quite painful indeed. I have to use INFO everywhere in numerics-heavy code to get meaningful output when a test fails.