Always false float comparisons

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Sat May 14 23:49:19 PDT 2016


On Saturday, 14 May 2016 at 20:41:12 UTC, Era Scarecrow wrote:
>  The real reason it's false is because you're comparing against 
> an immutable (constant), or my results lead me to believe that; 
> Otherwise why could cf and If work?

The "immutable" is of type real... Unfortunately the "cf" is also 
computing as real whereas "f" is implicitly converted to real 
from float. It makes comparing floating point types for equality 
implementation defined in D, i.e. unreliable.

The assignment to "cf" ought to have brought cf to 32 bit float.

So the "false" results are the correct ones, and the "true" are 
the unexpected ones where you compare different types.

import numpy
f=numpy.float32(1.30)
r=numpy.float64(1.30)
print r==f  # false



More information about the Digitalmars-d mailing list