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

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5565 - [64-bit] Wrong Floating Point Results, Related to Mixing With size_t
Summary: [64-bit] Wrong Floating Point Results, Related to Mixing With size_t
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
Reported: 2011年02月11日 22:06 UTC by David Simcha
Modified: 2011年02月12日 21:57 UTC (History)
1 user (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2011年02月11日 22:06:07 UTC
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
Comment 1 David Simcha 2011年02月11日 22:08:48 UTC
Forgot to mention: This test case doesn't reproduce the bug if -O is enabled, though the test case I reduced it from did, I think. (I'm not 100% sure that I wasn't just seeing two different bugs simultaneously.)
Comment 3 David Simcha 2011年02月12日 15:37:05 UTC
I think this was actually two bugs that I thought had a single root cause. You only got one of them. Now, the result from running this code with -m64 and without -O on linux is:
8
1	4545912	-4.54591e+06
-4.54591e+06
On the line with the three columns, the first one is now correct. The other two are still wrong, unless -O is enabled.


AltStyle によって変換されたページ (->オリジナル) /