www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5229] New: Inaccurate parsing of floating-point literals

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5229

           Summary: Inaccurate parsing of floating-point literals
           Product: D
           Version: D1 & D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bugzilla kyllingen.net


--- Comment #0 from Lars T. Kyllingstad <bugzilla kyllingen.net> 2010-11-17
03:29:34 PST ---
80-bit reals give you roughly 19 decimal digits of precision.  Thus, for a
given number, 20 digits should usually be enough to ensure that the literal
gets mapped to the closest representable number.

The following program shows that this is not always the case.  Here, 23 digits
is needed to get the closest representable number to pi^2, even though the
approximation to pi^2 itself is only accurate to 18 digits!

Test case:

    void main()
    {
        // Approximations to pi^2, accurate to 18 digits:
        real closest = 0x9.de9e64df22ef2d2p+0L;
        real next    = 0x9.de9e64df22ef2d3p+0L;

        // A literal with 23 digits maps to the correct
        // representation.
        real dig23 = 9.86960_44010_89358_61883_45L;
        assert (dig23 == closest);

        // 22 digits should also be (more than) sufficient,
        // but no...
        real dig22 = 9.86960_44010_89358_61883_5L;
        assert (dig22 == closest);  // Fails; should pass
        assert (dig22 == next);     // Passes; should fail
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 17 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5229


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #1 from Don <clugdbug yahoo.com.au> 2010-11-17 04:23:43 PST ---
Actually 19 digits works. The thing that's wrong is that the compiler uses
_all_ provided digits. Instead, according IEEE754, it should only take the
first 19 digits, performing decimal rounding of the l9th digit if more digits
are provided.

It's a problem in DMC's standard library implementation of strtold().

I once found a case where adding more decimal digits made the number smaller(!)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 17 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5229


Maxime Chevalier-Boisvert <maximechevalierb gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maximechevalierb gmail.com


--- Comment #2 from Maxime Chevalier-Boisvert <maximechevalierb gmail.com>
2013-02-14 19:08:40 PST ---
Floating-point parsing also fails on this much shorter number:

4294967295.0 
parses to:
4294967296.0

When parsed using:
double val = to!(double)(numStr);

Also fails with formattedRead.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 14 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5229



--- Comment #3 from Lars T. Kyllingstad <bugzilla kyllingen.net> 2013-02-15
00:44:29 PST ---
Maxime: What you're describing looks like a bug in Phobos, while this report is
about a bug in DMD.  You should probably create a new report for that one.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5229


David Nadlinger <code klickverbot.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code klickverbot.at


--- Comment #4 from David Nadlinger <code klickverbot.at> 2013-05-20 14:30:02
PDT ---
 Lars: If she is using DMD on Windows, the root cause might in fact be the
same.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 20 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5229



--- Comment #5 from Maxime Chevalier-Boisvert <maximechevalierb gmail.com>
2013-05-20 14:51:28 PDT ---
(In reply to comment #4)
  Lars: If she is using DMD on Windows, the root cause might in fact be the
 same.
It turned out to be a problem with my code. I apologize for not testing thorougly enough before commenting. My comment should be removed from this thread if possible. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 20 2013