[Issue 14958] Casting a double to ulong sometimes produces wrong results

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Aug 25 10:52:08 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14958

--- Comment #4 from Steven Schveighoffer <schveiguy at yahoo.com> ---
(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:

double x = 1.2;
auto y = x * 10.0;
pragma(msg, typeof(y)); // double
pragma(msg, typeof(x * 10.0)); // double
writefln("%s %s", cast(ulong)y, cast(ulong)(x * 10.0)); // 12 11

Clearly, treating the result as a double should result in 12. But it doesn't.
And there's no rhyme or reason why 'y' above should be any different from the
result of the expression (x * 10.0).

This violates the principal of refactoring calculations by assigning them to
variables. I don't want the result of my code to change if I have to create a
temporary using auto.

Two ways to fix are: 1) treat the result of an expression typed as a double as
a double (perform the rounding) or 2) typeof(double op double) should be real.

I'm not going to reopen, because I don't have any personal interest in fighting
for this. But I can definitely see where D has gone wrong here.

--


More information about the Digitalmars-d-bugs mailing list