4 ms·
If I understand correctly, it's a printf-style function for which the %g format code outputs the shortest decimal string that round trips. This implies that it
by SloopJon 6y ago
If I understand correctly, it's a printf-style function for which the %g format code outputs the shortest decimal string that round trips. This implies that it would output no more than seventeen significant figures, regardless of the requested precision.
The first thing I notice is this comment in a later changelog:
> I believe we have a very fast numerical conversion that appears to be reliably generating about 15 to 16 digits out output [last digit may be off by 1 or 2 in typical case].
If the last digit of a fifteen- to sixteen-digit output is off by one or two, are we sure it round trips?
Second, the test coverage for this feature in tests/format.cpp looks pretty sparse, and it's not obvious to me how the output is verified.
Interesting find. I may play around with this later.
Edit: I tried the test cases from here:
https://www.exploringbinary.com/the-shortest-decimal-string-that-round-trips-examples/ https://www.exploringbinary.com/the-shortest-decimal-string-...
Here's the result:
format="%.30g" output="0.1"
format="%.30g" output="50388143.06823721984"
format="%.30g" output="54167628.18"
format="%.30g" output="9161196241250.051072"
The first and third cases output the correct string. The second case outputs nineteen digits, instead of seventeen, and does not round trip (i.e., it rounded incorrectly). The last case outputs nineteen digits instead of fifteen, but does round trip.
- MaxBarraclough 6y agoNice work, thanks for looking into it.