Open sigbjorn opened 2 days ago
Many thanks for bringing thisn up. If you do a PR of the preferred solution, that would be wonderful!
I will make PR for it along the directions hinted, just need some time available, also to verify it works on some more compilers as well.
I will make PR for it along the directions hinted, just need some time available, also to verify it works on some more compilers as well.
Sounds good!
PR and proposal added, based on develop branch, seems that the two failing jobs are not related to the PR
Overview
As detected reported on Shyft open source, https://gitlab.com/shyft-os/shyft/-/issues/1216,
Wrong outputs are generated for numbers of the form 1.09e-6, instead of generating expected '1.09e-06', it outputs 1.9e-06 -lacking trailing zeros before the 9, so an entirely different number, not resembling the input (like rounding errors etc).
This is due to an edge case not detected in the test-setup, where the passed n to the fraction routine is of the form:
9999.00000005 or similar.
How to reproduce
Adding the following test snippet to the test-suite, karma/real3.cpp:
Failes the test, with output
Possible solutions
Minimal
The minimal fix (in terms bytes), is to use floor(n) in the digit expression to get rid of trailing digits that promotes to computing one to much digits.
real_policies.hpp
The idea is to remove the fraction part of the fraction passed (so 9.0000.....5 becomes 9.0 and the log10..ceil provides 1 in stead of 2, and thus one 0 is injected before the 9 digit.
A bit more verbose solution
Other approach is based on example provided by other issues around the same function, where the computation of digits needed to format an int is factored out.
A mod of that example, in-lined, and written so that it preserves the real_concept used in the tests, so that the test-suite passes, could look like this:
Performance indications
Using the performance test, examples, for generating double to strings,
modified to use fmt v11,
boost 1.86, arch linux, gcc 14.2.1
original double_ and the two variants above gives:
Workarounds
Thanks to customization points, the above fixes can be done using existing boost library, and overriding the generator policy with the preferred solution while waiting for fixes to be part of next.