Thread overview
[Issue 20320] format("%f") leeds to wrong output
Oct 25, 2019
berni44
Oct 25, 2019
kinke
Nov 08, 2019
berni44
Nov 10, 2019
Dlang Bot
Jan 20, 2021
Dlang Bot
Feb 15, 2021
Dlang Bot
Apr 11, 2021
Dlang Bot
Apr 12, 2021
Dlang Bot
October 25, 2019
https://issues.dlang.org/show_bug.cgi?id=20320

--- Comment #1 from berni44 <bugzilla@d-ecke.de> ---
Some more debug information (from Yui Hosaka):

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]);
}

The outputs for your program are as follows:

Without compiler options (32-bit):
---
0.01
3b df 4f 8d 97 6e 12 83 f9 3f
0.01
---
and some trailing characters. "./a | od -c" shows:
---
0000000   0   .   0   1  \r  \n   3   b       d   f       4   f
    8
0000020   d       9   7       6   e       1   2       8   3       f   9
0000040       3   f      \r  \n   0   .   0   1 377 377 206 003  \r  \n
0000060
---

With "-O" (32-bit), the output changes in every run, such as:
---
0.01
3b df 4f 8d 97 6e 12 83 f9 3f
0.01S
---
0.01
3b df 4f 8d 97 6e 12 83 f9 3f
0.01b
---
0.01
3b df 4f 8d 97 6e 12 83 f9 3f
0.01c
---

With "-m64" or "-m64 -O":
---
0.02
3b df 4f 8d 97 6e 12 83 f9 3f
0.00
---

For me, this looks like the error is to be found inside snprintf. The "0.00" in the last run is strange.

--
October 25, 2019
https://issues.dlang.org/show_bug.cgi?id=20320

kinke <kinke@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net

--- Comment #2 from kinke <kinke@gmx.net> ---
Microsoft's snprintf doesn't support 80-bit real, as Microsoft's `long double` is a 64-bit double. So snprintf can't be used on Windows with -m64 and -m32mscoff. No idea about DigitalMars' snprintf.

--
November 08, 2019
https://issues.dlang.org/show_bug.cgi?id=20320

--- Comment #3 from berni44 <bugzilla@d-ecke.de> ---
I meanwhile was able to reproduce this in PR #7264 (https://github.com/dlang/phobos/pull/7264). This PR fixes this for float and double but not real.

--
November 10, 2019
https://issues.dlang.org/show_bug.cgi?id=20320

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@berni44 updated dlang/phobos pull request #7264 "Partial replace call to snprintf for formating floatingpoint numbers." mentioning this issue:

- Fix partially Issue 20320 - format("%f") leeds to wrong output

https://github.com/dlang/phobos/pull/7264

--
January 20, 2021
https://issues.dlang.org/show_bug.cgi?id=20320

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@berni44 created dlang/phobos pull request #7757 "Partial replace call to snprintf for formating floatingpoint numbers for %f and %F" mentioning this issue:

- Fix partially Issue 20320 - format("%f") leeds to wrong output

https://github.com/dlang/phobos/pull/7757

--
February 15, 2021
https://issues.dlang.org/show_bug.cgi?id=20320

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/phobos pull request #7757 "Partial replace call to snprintf for formating floatingpoint numbers for %f and %F" was merged into master:

- 782780a3967873af468809445a4d7809a2d925c7 by Bernhard Seckinger:
  Fix partially Issue 20320 - format("%f") leeds to wrong output

https://github.com/dlang/phobos/pull/7757

--
April 11, 2021
https://issues.dlang.org/show_bug.cgi?id=20320

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
@berni44 created dlang/phobos pull request #7951 "std.format: Replace snprintf for x87-reals" fixing this issue:

- Fix Issue 20320 - format("%f") leeds to wrong output

https://github.com/dlang/phobos/pull/7951

--
April 12, 2021
https://issues.dlang.org/show_bug.cgi?id=20320

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #8 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/phobos pull request #7951 "std.format: Replace snprintf for x87-reals" was merged into master:

- 23e5c9e15086c87f9160a0b594132eb60b338bb2 by berni44:
  Fix Issue 20320 - format("%f") leeds to wrong output

https://github.com/dlang/phobos/pull/7951

--