`std.format.formatValue` uses C's `snprintf` which works this way in `snn.lib`: --- import std.string; void main() { assert(format("%.1f", 0.09) == "0.1"); // Failed, formatted as "0.0" assert(format("%.1f", -0.09) == "-0.1"); // Failed, formatted as "-0.0" assert(format("%.1f", 0.095) == "0.1"); // OK assert(format("%.1f", -0.095) == "-0.1"); // OK assert(format("%.1f", 0.094) == "0.1"); // Failed, formatted as "0.0" assert(format("%.1f", -0.094) == "-0.1"); // Failed, formatted as "-0.0" } --- Workaround: --- real preformatFloating(real n, ubyte digits) { immutable k = 10.0L ^^ digits; return round(n * k) / k; } void main() { assert(format("%.1f", preformatFloating(0.09, 1)) == "0.1"); } ---
Tested on DMD v2.066.1, this runs fine. import std.string; void main() { assert(format("%.1f", 0.09) == "0.1"); // Failed, formatted as "0.0" assert(format("%.1f", -0.09) == "-0.1"); // Failed, formatted as "-0.0" assert(format("%.1f", 0.095) == "0.1"); // OK assert(format("%.1f", -0.095) == "-0.1"); // OK assert(format("%.1f", 0.094) == "0.1"); // Failed, formatted as "0.0" assert(format("%.1f", -0.094) == "-0.1"); // Failed, formatted as "-0.0" } Report was on windows, I tested in on linux, hopefully format() isnt os specific. Report mentions std.format.formatValue but doesnt test it.
Closing preemptively according to http://forum.dlang.org/thread/fsmjexsfxtqveqffiemc@forum.dlang.org#post-ybwwdcpzianfozqcflcv:40forum.dlang.org. Please reopen if necessary, thanks.
As the original comment states, this is specific to snn.lib (digital mars' c runtime) and therefore win32.
Thanks @yebblies!
(In reply to Andrei Alexandrescu from comment #2) > Closing preemptively according to > http://forum.dlang.org/thread/fsmjexsfxtqveqffiemc@forum.dlang.org#post- > ybwwdcpzianfozqcflcv:40forum.dlang.org. Please reopen if necessary, thanks. Wat has just happened here? There was a Windows-specific bug report. A person who isn't fimilar with Phobos and lokks like a newbie (not his fault by the way) tested it on Linux, said "testcase works" and changed OS to Linux, also proving the fact he has no idea how does formatting work by writing "format() isnt os specific" and "Report mentions std.format.formatValue but doesnt test it". And based on this the issue was closed by a Phobos developer. So I'd like to ask Phobos devs: please, if your are tired and out of time, don't do thoughtless actions. In case something probably can be easily closed/fixed etc. please ask issue reporter or another person who has more time to induct because such actions in the worst case may remain unnoticed. Thanks.
(In reply to Denis Shelomovskij from comment #5) > > Wat has just happened here? > > There was a Windows-specific bug report. A person who isn't fimilar with > Phobos and lokks like a newbie (not his fault by the way) tested it on > Linux, said "testcase works" and changed OS to Linux, also proving the fact > he has no idea how does formatting work by writing "format() isnt os > specific" and "Report mentions std.format.formatValue but doesnt test it". > And based on this the issue was closed by a Phobos developer. > > So I'd like to ask Phobos devs: please, if your are tired and out of time, > don't do thoughtless actions. In case something probably can be easily > closed/fixed etc. please ask issue reporter or another person who has more > time to induct because such actions in the worst case may remain unnoticed. > Thanks. The original reporter is CC'd when an issue is closed or updated, so mistakes can easily be corrected. It's not a big deal, just a mistake.
Adjusting arch to be X86 only according to Daniel's comment. One thing I would mention -- it's not readily apparent that any of the fields above have changed since the original report, the change is logged but you have to look at bug history to see the change. Please avoid changing them unless you are certain that the change is correct. AndyC, I appreciate you combing through old bugs, so please keep going!
@berni44 updated dlang/phobos pull request #7264 "Partial replace call to snprintf for formating floatingpoint numbers." fixing this issue: - Fix Issue 9889 - Incorrect rounding on floating value formatting https://github.com/dlang/phobos/pull/7264
@berni44 created dlang/phobos pull request #7757 "Partial replace call to snprintf for formating floatingpoint numbers for %f and %F" fixing this issue: - Fix Issue 9889 - Incorrect rounding on floating value formatting https://github.com/dlang/phobos/pull/7757
dlang/phobos pull request #7757 "Partial replace call to snprintf for formating floatingpoint numbers for %f and %F" was merged into master: - 3767b6c06af271f6aa72cbfbc7a210e582401447 by Bernhard Seckinger: Fix Issue 9889 - Incorrect rounding on floating value formatting https://github.com/dlang/phobos/pull/7757