Catch2
Catch2 copied to clipboard
Allow changing the format of StringMaker<float> and StringMaker<double>
Description
Allows the user to change the output of StringMaker<float> and StringMaker<double> from std::fixed to std::scientific.
This can be useful when comparing very small numbers, which in fixed notation requires counting zeros, but in scientific notation is clear.
The user can make this change by setting the static format variable, e.g. Catch::StringMaker<float>::format = std::scientific;.
Codecov Report
Merging #2226 (f8132e6) into devel (2cb5210) will not change coverage. The diff coverage is
100.00%.
@@ Coverage Diff @@
## devel #2226 +/- ##
=======================================
Coverage 90.05% 90.05%
=======================================
Files 146 146
Lines 7099 7099
=======================================
Hits 6393 6393
Misses 706 706
Does this seem like a reasonable addition? Should any changes be made to make it suitable for merging?
@horenmar, any chance you could take a quick look at this PR? Thanks!
As pointed out in https://github.com/catchorg/Catch2/issues/2238#issuecomment-1261152406, the removal of trailing zeros in fpToString would cause trailing zeros in the exponent to be cut off. So as suggested there, this part should be bypassed if format != std::fixed.
Other than that I really think this pull request should be considered. Scientific output seems pretty vital to me for getting reliably meaningful test output with any non-trivial floating point data. Apart from the issue with small values as pointed out in #2238, for large numbers an excessive amount of meaningless digits will be printed for high precisions with std::fixed.