www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9889] New: Incorrect rounding on floating value formatting

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 puremagic.com
        ReportedBy: verylonglogin.reg gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg 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: -------
Apr 06 2013