Inconsistent floating point conversion to bool
rumbu
rumbu at rumbu.ro
Sat Dec 9 11:51:10 UTC 2017
float f = float.min_normal;
bool fcast1 = cast(bool)f;
bool fcast2 = cast(bool)float.min_normal;
if (fcast1)
writeln("variable casting to bool is true");
else
writeln("variable casting to bool is false");
if (fcast2)
writeln("constant casting to bool is true");
else
writeln("constant casting to bool is false");
if (float.min_normal)
writeln("implicit conversion is true");
else
writeln("implicit conversion is false");
variable casting to bool is true
constant casting to bool is false
implicit conversion is true
Now, casting any float constant in the interval (-1.0;1.0) to
bool will return false, but the same value considered as an if
condition or variable will always return true;
Looking at disassembly, fcast2 is directly initialized to false,
the compiler does not emit any conversion code:
bool fcast2 = cast(bool)float.min_normal;
004174fa mov byte [ebp-0x3], 0x0
Is this intended behavior or it's a bug?
More information about the Digitalmars-d
mailing list