bug? floating point precision with std.format

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 5 10:23:38 PDT 2017


On 6/5/17 12:29 PM, Seb wrote:
> You do realize that you have used "s" in the D version?

Yes, I thought it was a stand in for "use the type to determine the 
specifier", and I mistakenly assumed that would be 'f', since that's 
what I've always used for floating point. Apparently it is 'g', which 
behaves as I have shown.

> This is a bit more interesting:
>
> writefln("%0.3s", f); // 20.7
> printf("%0.3s\n", f); // 20.

That is really bad, because %s means interpret the parameter as a char * 
string. So the memory pointed at by the bit pattern of 20.66666 cast to 
a pointer, has the first 3 bytes '2', '0', and '.', and then a null 
character (or a bunch of unprintable characters, followed by a null).

Bizarre...

-Steve


More information about the Digitalmars-d mailing list