Precision of double in toJSON(and in general)

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 24 03:06:55 PST 2016


http://dpaste.dzfl.pl/9a0569b20756

toJSON is converting to the default precision, that fits `float`, 
even though the actual type is `double`. In general, printing 
`double` with "%s" fits `float` more than `double`.

The problem, as I understand it, is that "%s" is converted to 
"%g" instead of "%f" for both `float`s and `double`s. I assume 
that's because "%f" is always printed in fixed-point format, 
while "%g" can choose between the fixed-point and the scientific 
formats.

This is all well for regular printing, but when converting to 
JSON we needlessly lose precision. Wouldn't it be better to 
convert to JSON using "%f", or to generally increase the 
precision of `double` when using "%s"?


More information about the Digitalmars-d mailing list