Floating point printing should print shortest round-trip value
- Dominant language
- C++
- Stars
- 21.5k
- Forks
- 3.5k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 2
Description
**Description**
Currently, Catch2 uses iostream with fixed precision for printing `float` and `double`. This is fine, until it's not. Consider comparing two `double`s that are 1 ULP apart:
```cpp
CHECK(5.000000000000001e-05 == 5e-05);
```
This fails with the amusingly unhelpful:
```cpp
/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::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`):
|precision|output|
|-|-|
|19|`0.00005 == 0.00005`|
|20|`0.00005000000000000001 == 0.00005`|
|21|`0.000050000000000000009 == 0.000050000000000000002`|
fmt|`5.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](https://github.com/vitaut/zmij).
Contributor guide
Research direction
Start at Catch::StringMaker::precision and inspect the current iostream formatting. Compare the provided 1-ULP example with std::to_chars or another supported formatting approach, while checking Catch2's compiler requirements. Done means floating-point output shows the shortest round-trip values without hiding distinct values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100