www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - to!double different from cast(double)

reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jan 04 2016
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 01/04/2016 12:22 AM, Ali Çehreli wrote:

      assert(l != l.to!double);      // passes
      assert(l != cast(double)l);    // FAILS
I've realized that I had the -m32 switch on unintentionally. The above results are for when -m32 is used. (I am on a 64-bit system.) Without -m32 both checks fail and the universe makes sense. Still, would you consider it a bug that the results are different for -m32? Ali
Jan 04 2016
prev sibling parent anonymous <anonymous example.com> writes:
On 04.01.2016 09:22, Ali Çehreli wrote:
 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.)
I suspect this is due to D allowing floating point operations to happen with higher precision than requested by the program. The comparisons may be done with `real` precision, and `l.to!double` is cut down to double precision while `cast(double)l` is not.
Jan 04 2016