digitalmars.D - Inconsistent floating point conversion to bool
- rumbu (26/26) Dec 09 2017 float f = float.min_normal;
- Iain Buclaw (3/29) Dec 09 2017 Raise a bug if you are unsure.
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?
Dec 09 2017
On 9 December 2017 at 12:51, rumbu via Digitalmars-d
<digitalmars-d puremagic.com> wrote:
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?
Raise a bug if you are unsure.
Dec 09 2017








Iain Buclaw <ibuclaw gdcproject.org>