[Issue 5565] New: [64-bit] Wrong Floating Point Results, Related to Mixing With size_t

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 11 22:08:37 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5565

           Summary: [64-bit] Wrong Floating Point Results, Related to
                    Mixing With size_t
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: dsimcha at yahoo.com


--- Comment #0 from David Simcha <dsimcha at yahoo.com> 2011-02-11 22:06:07 PST ---
The following code produces erratic results.  I don't know where exactly the
culprit is, but I'm hoping I've reduced it enough that someone with a decent
mental model of how the compiler works will be able to finish the job.

import std.stdio;

double foo(int[] data, double q = 0.25) {
    immutable double N = data.length;

    // This actually affects the result, making it more severe and obvious
    // in addition to showing that the value is correct.
    stderr.writeln(N);  // Prints 8.  Correct.

    // lowEnd should be floor(7 * 0.25) == 1.
    immutable lowEnd = cast(size_t) ((N - 1) * q);

    // highEnd should be floor(7 * 0.75) - 1 = 4.
    immutable highEnd = cast(size_t) ((N - 1) * (1.0 - q) - lowEnd);

    // highFract should be 7 * 0.75 - 4 - 1 = 0.25
    immutable highFract = (N - 1) * (1.0 - q) - lowEnd - highEnd;

    stderr.writeln(lowEnd, '\t', highEnd, '\t', highFract);
    return highFract;
}

void main() {
    writeln(foo([1,2,3,4,5,6,7,8]));
}

Prints:

8
0    4545916    -4.54591e+06
-4.54591e+06

-- 
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