Inconsistent floating point conversion to bool
Iain Buclaw
ibuclaw at gdcproject.org
Sat Dec 9 12:52:42 UTC 2017
On 9 December 2017 at 12:51, rumbu via Digitalmars-d
<digitalmars-d at 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.
More information about the Digitalmars-d
mailing list