Bug 18054 – Wrong cast of float constant to bool

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-12-10T07:49:00Z
Last change time
2021-12-14T10:34:10Z
Keywords
pull
Assigned to
No Owner
Creator
Răzvan Ștefănescu

Comments

Comment #0 by rumbu — 2017-12-10T07:49:00Z
Any float constant between -1 and 1 cast to bool will return false: float f = float.min_normal; bool fcast1 = cast(bool)f; bool fcast2 = cast(bool)(float.min_normal); assert(fcast1); //ok assert(fcast2); //error;
Comment #1 by aldacron — 2021-11-11T08:27:40Z
This also happens with double & real, and literals. auto d = 0.5; assert(cast(bool)d); assert(cast(bool)0.5);
Comment #2 by dlang-bot — 2021-11-11T11:07:01Z
@MoonlightSentinel created dlang/dmd pull request #13285 "Fix 18054 - Use toBool instead of toInteger when const-folding cast(b…" fixing this issue: - Fix 18054 - Use toBool instead of toInteger when const-folding cast(bool) `RealExp` / `ComplexExp` may hold values that don't have a matching integer value (unlike `IntegerExp`). Switching to `toBool` ensures proper boolean conversion according to the current type. Teh test output shown below (before this change) was caused by the invalid const-folding for floating point literals. ``` float: 1 == 0 double: 1 == 0 real: 1 == 0 ifloat: 1 == 0 idouble: 1 == 0 ireal: 1 == 0 cfloat: 1 == 0 cdouble: 1 == 0 creal: 1 == 0 ``` https://github.com/dlang/dmd/pull/13285
Comment #3 by dlang-bot — 2021-11-12T01:47:57Z
dlang/dmd pull request #13285 "Fix 18054 - Use isBool instead of toInteger when const-folding cast(bool)" was merged into stable: - ddc6ff08631e221db1b83eca1a07c3d98e4cc9a9 by MoonlightSentinel: Fix 18054 - Use isBool instead of toInteger when const-folding cast(bool) `RealExp` / `ComplexExp` may hold values that don't have a matching integer value (unlike `IntegerExp`). Switching to `toBool` ensures proper boolean conversion according to the current type. The test output shown below (before this change) was caused by the invalid const-folding for floating point literals. ``` float: 1 == 0 double: 1 == 0 real: 1 == 0 ifloat: 1 == 0 idouble: 1 == 0 ireal: 1 == 0 cfloat: 1 == 0 cdouble: 1 == 0 creal: 1 == 0 ``` https://github.com/dlang/dmd/pull/13285
Comment #4 by dlang-bot — 2021-12-14T10:34:10Z
dlang/dmd pull request #13421 "merge stable" was merged into master: - 75b9a2334042d8b26055b6ad2bc4b7c3605411c5 by Florian: Fix 18054 - Use isBool instead of toInteger when const-folding cast(bool) (#13285) `RealExp` / `ComplexExp` may hold values that don't have a matching integer value (unlike `IntegerExp`). Switching to `toBool` ensures proper boolean conversion according to the current type. The test output shown below (before this change) was caused by the invalid const-folding for floating point literals. ``` float: 1 == 0 double: 1 == 0 real: 1 == 0 ifloat: 1 == 0 idouble: 1 == 0 ireal: 1 == 0 cfloat: 1 == 0 cdouble: 1 == 0 creal: 1 == 0 ``` https://github.com/dlang/dmd/pull/13421