Closed otrho closed 10 months ago
I do remember there was a reason I had it set to 6 decimal places by default instead of the usual hack for round tripping with %.17g, but I admit I don't recall. There is also the %j
format specifier that will use %.17g as suggested here.
Anyway, lgtm. I think the reason was that there were issues in the accuracy of code in older versions of glibc with snprintf - atof and snprintf were not perfect inverses of each other resulting in entering 1.1
resulting in extra garbage in the repl.
EDIT: Languages like python I think package their own snprintf. Janet has its own strtod (for custom bases besides 10), but not printf.
crose@bettybox:~$ janet
Janet 1.31.0-4139e426 linux/x64/gcc - '(doc)' for help
repl:1:> (setdyn *pretty-format* "%j")
"%j"
repl:2:> 1.1
1.1000000000000001
repl:3:>
Which is annoying to say the least. I have checked with a few tools and the "error" seems to be in snprintf. Error in quotes because I think the inaccuracy is probably allowed by the C standard.
Anyway, lgtm. I think the reason was that there were issues in the accuracy of code in older versions of glibc with snprintf - atof and snprintf were not perfect inverses of each other resulting in entering
1.1
resulting in extra garbage in the repl.
Yar, the pretty printing seems to be used quite a bit all over the place, including the repl of course, so I wasn't sure if it was going to break some assumptions. But either way, via some other edge case handling if need be, IMO saying '123456' isn't an integer is confusing.
Fixed by f18ad36b1bd22a3b2a0888472b46bed9c252e65e instead.
Issue.
I was a bit confused when I came across the following error:
I didn't know that my index (
123456.5
in the repro) was a double, and the error is saying it expects an integer, and it got123456
which looks very much like an integer.Solution?
The problem seems to be the choice in using
%g
innumber_to_string()
inpp.c
. If we use%.17g
instead it will always print all the significant digits for a double with a 53-bit mantissa, and never round to what looks like an integer.I noticed below in
pp.c
, inprint_jdn_one()
there is already a%.17g
in use.Here's a handy little snippet to help illustrate:
At the line marked 'HERE' you can see the
%g
is rounding to 6 significant digits and dropping the fractional part. Annoying.Caveat
I didn't add any new testing for it, not sure it needs it. I also don't have access to a Windows box and couldn't test it myself, to check if
%.17g
works as expected there.