printf() metaprogramming challenge

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sat May 25 07:10:28 UTC 2019


On Friday, 24 May 2019 at 22:59:10 UTC, Walter Bright wrote:
> https://github.com/ulfjack/ryu says: "The Java implementation 
> differs from the output of Double.toString in some cases: 
> sometimes the output is shorter (which is arguably more 
> accurate) and sometimes the output may differ in the precise 
> digits output" which I find fairly concerning. Please review 
> the paper I linked to in my reply to Jacob.

AFAIK, Ulf Adams is stating that the Java implementation is 
sloppy (my word).

He states that other implementations provide more digits than is 
necessary to get an accurate representation.

https://dl.acm.org/citation.cfm?id=3192369

> C's printf has been hammered on by literally generations of 
> programmers over 3 decades. While the interface to it is 
> old-fashioned and unsafe, the guts of it are rock solid, fast, 
> and correct.

Not really. He argues that the C spec isn't clear, so he follows 
a more stringent criteria than C printf.


Burger and Dybvig found errors in implementations from DEC, HP 
and SGI:
https://www.cs.indiana.edu/~dyb/pubs/FP-Printing-PLDI96.pdf


Others claim to find roundoff errors in a common printf 
implementations e.g.:«The implementation that ships with 
Microsoft Visual C++ 2010 Express sometimes has an off-by-one 
error when rounding to the closest value.»

http://www.ryanjuckett.com/programming/printing-floating-point-numbers/

  (I haven't checked the claim, but it would not surprise me).


It is clear that not using C standard lib will bring more 
consistent and portable results across platforms, even the C 
version is correct as the C-standard leaves wiggle room.  This 
can be important in scientific computing when comparing results 
from various platforms.



More information about the Digitalmars-d mailing list