[Issue 14958] Casting a double to ulong sometimes produces wrong results
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Aug 25 20:16:54 PDT 2015
https://issues.dlang.org/show_bug.cgi?id=14958
--- Comment #5 from yebblies <yebblies at gmail.com> ---
(In reply to Steven Schveighoffer from comment #4)
> (In reply to yebblies from comment #3)
> > This is working to spec. Please re-open as an enhancement request if you
> > feel it's necessary.
>
> I think the expectation is violated when there is a measurable difference
> between treating a value as a real and treating it as a double, and the
> compiler is doing things under the hood that cannot be detected that force
> the calculation to be done with reals. For instance:
>
As I said, if you want to use FPU hardware then the exact results will depend
on exact instruction selection, which depends on inlining and register
allocation. Rounding at every step is not possible in a performant way. So
since we can't guarantee that two identical expressions get evaluated the same
way at runtime, it doesn't make any sense to guarantee a maximum precision at
compile time.
eg
double x = someDouble * someOtherDouble;
double y = x + something; // this becomes y = mad(someDouble, someOtherDouble,
something)
// some code that results in registers being spilled to stack
double z = x + something; // this doesn't get turned into mul+add, because the
compiler decides it's better to just load x and add
Now y and z may have different values thanks to increased internal precision of
multiply and add.
--
More information about the Digitalmars-d-bugs
mailing list