Printing floating point numbers

berni44 dlang at d-ecke.de
Fri Oct 25 06:53:38 UTC 2019


On Thursday, 24 October 2019 at 20:48:02 UTC, Yui Hosaka wrote:
> Do you have any idea for this issue?

I added a bug report: 
https://issues.dlang.org/show_bug.cgi?id=20320

Internally the conversation from the binary representation of the 
value to the printed one is done by a call to a C function called 
snprintf. Probably the error is inside of this function call. But 
it could also happen before. Maybe the internal representation of 
0.016 is allready wrong.

It would be helpful if you could run the following program and 
post the output:

import std.stdio;
import std.format;
void main()
{
     real b = 0.016;
     writefln!"%.2f"(b);
     foreach (c;format("%r",b)) writef("%x ",c);
     writeln();

     char[6] sprintfSpec = "%*.*Lf";
     char[512] buf = void;
     import core.stdc.stdio : snprintf;
     immutable n = snprintf(buf.ptr, buf.length, sprintfSpec.ptr, 
0, 2, b);
     writeln(buf[0..n]);
}



More information about the Digitalmars-d-learn mailing list