W0005Comparison with
WarningSafe fix
Comparison with f64.nan
WarningSafe fixA comparison == or != with f64.nan on either side. nan is not equal to any number, itself included, so x == f64.nan is always false and x != f64.nan always true. The warning points at the comparison, and the program still builds. To test whether x is nan, call x.is_nan().
Why
x == f64.nan reads as the test that x is nan, and it never is one: IEEE 754 defines every comparison with nan as false, except !=. The same holds in Rust, C and Python. When the other side is an f64, the safe fix writes x.is_nan(), or !x.is_nan() for !=, with parentheses around an x that needs them. When it is an f64?, there is no fix, because x.is_nan() would not check: unwrap the value first.
Broken
fn main() {
let x = 0.5
print(x == f64.nan)
}
What sisu says
warning[W0005]: comparison with `f64.nan` is always false --> W0005.sisu:3:11 | 3 | print(x == f64.nan) | ^^^^^^^^^^^^ | = help: `nan` is not equal to any number, itself included; test for it with `is_nan()` help: test it with `is_nan()` | 3 - print(x == f64.nan) 3 + print(x.is_nan()) | sisu: 1 warning sisu: for more on a code, run `sisu explain <code>`: W0005
Fixed
fn main() {
let x = 0.5
print(x.is_nan())
}
Related
- E0105: a type used as a value.
Sisu