Catch2 icon indicating copy to clipboard operation
Catch2 copied to clipboard

Allow changing the format of StringMaker<float> and StringMaker<double>

Open pazner opened this issue 4 years ago • 8 comments

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;.

pazner avatar Apr 29 '21 21:04 pazner

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           

codecov[bot] avatar Apr 29 '21 22:04 codecov[bot]

Does this seem like a reasonable addition? Should any changes be made to make it suitable for merging?

pazner avatar Jun 18 '21 00:06 pazner

@horenmar, any chance you could take a quick look at this PR? Thanks!

pazner avatar Aug 16 '21 16:08 pazner

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.

mjacobse avatar Jun 09 '23 15:06 mjacobse