to!double different from cast(double)

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 4 00:22:11 PST 2016


I was writing an example to show that not every 'long' value can be 
represented by a 'double'. (They are both 64 bits; but for 'double', 
some of those bits are parts of the exponent, not the mantissa.)

Although my demonstration works with to!double, the compiler does 
something different and the cast(double) test fails:

import std.conv;

void main() {
     const l = long.max;

     assert(l != l.to!double);      // passes
     assert(l != cast(double)l);    // FAILS
}

Is there a good explanation for this difference? I would expect both 
expressions to be compiled the same way. (I am aware that the != 
operator takes two doubles, meaning that the left-hand side is converted 
to 'double' before the comparison.)

Ali


More information about the Digitalmars-d-learn mailing list