D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3947 - Implicit and explicit casting of floating point to bool produces different results
Summary: Implicit and explicit casting of floating point to bool produces different re...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2010-03-13 01:50 UTC by Aldo Nunez
Modified: 2024-12-13 17:51 UTC (History)
2 users (show)

See Also:


Attachments
test case with comments (7.67 KB, application/octet-stream)
2010-03-13 01:50 UTC, Aldo Nunez
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Aldo Nunez 2010-03-13 01:50:09 UTC
Created attachment 587 [details]
test case with comments

I've observed that these are the rules for explicitly casting floating point
numbers to bool:

- finite real numbers > -1 and < 1 are false
- all other real number values are true (including NaN)
- all imaginary numbers are false, since their real part is 0
- complex numbers as bool are the same as their real part as bool

I've noticed that the rules for implicitly casting floating point numbers to
bool, as in the expression (a && b), is different in several ways:

- real 0 and -0 are false
- all other finite real numbers > -1 and < 1 are *true*
- all other real number values are true (including NaN)
- imaginary 0 and -0 are false
- finite imaginary numbers > -1 and < 1 are *true*
- all other real number values are *true* (including NaN)
- complex numbers as bool are the same as the real *or* imaginary part as bool
under this new scheme. If either evaluates to true, then the complex is true.

There should be a single set of rules used for both cases.
As it is, I don't know which part of each set is right.
Comment 1 Nick Treleaven 2024-06-03 15:16:00 UTC
> - finite real numbers > -1 and < 1 are false

That is not the case, at least for all compilers currently on run.dlang.io:

    static assert(cast(bool) 0.1f);

Built-in complex numbers are now deprecated.
Comment 2 dlangBugzillaToGithub 2024-12-13 17:51:43 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18182

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB