to!double different from cast(double)

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 4 05:32:35 PST 2016


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.


More information about the Digitalmars-d-learn mailing list