December 09, 2017 Inconsistent floating point conversion to bool | ||||
---|---|---|---|---|
| ||||
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? |
December 09, 2017 Re: Inconsistent floating point conversion to bool | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | 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.
|
Copyright © 1999-2021 by the D Language Foundation