[Issue 9889] New: Incorrect rounding on floating value formatting
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Apr 6 02:51:20 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9889
Summary: Incorrect rounding on floating value formatting
Product: D
Version: D2
Platform: All
OS/Version: Windows
Status: NEW
Keywords: wrong-code
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: verylonglogin.reg at gmail.com
--- Comment #0 from Denis Shelomovskij <verylonglogin.reg at gmail.com> 2013-04-06 13:51:19 MSD ---
`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");
}
---
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list