printf() metaprogramming challenge

Jonathan Marler johnnymarler at gmail.com
Fri May 24 23:55:13 UTC 2019


On Friday, 24 May 2019 at 22:59:10 UTC, Walter Bright wrote:
> On 5/24/2019 2:07 PM, Jonathan Marler wrote:
>> It took me about an hour to port this "float to string" 
>> implementation to D:
>> 
>> https://github.com/ulfjack/ryu
>> https://github.com/dragon-lang/mar/blob/master/src/mar/ryu.d
>
> 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.
>
> Floating point formatting is not something that can be knocked 
> out in an hour. You can get a "mostly working" implementation 
> that way, but not a serious, robust, correct implementation 
> with the expected flexibility. (And the test cases to prove it 
> correct.)
>
> The fact that people write academic papers about it should be 
> good evidence.
>
> 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.

I didn't design an implementation in an hour, I just ported one :)

Ulf's algorithm can be implemented in only a few hundred lines 
and apparently is the fastest implementation to-date that 
maintains a 100% robust algorithm. At least that what I remember 
from watching his video.

https://pldi18.sigplan.org/details/pldi-2018-papers/20/Ry-Fast-Float-to-String-Conversion

He explains in the video why this is a hard problem and tries to 
explain his paper/algorithm.  But's it's very new, only a year 
old I think.  Cool innovation.


More information about the Digitalmars-d mailing list