Home / Docs / Diagnostics
U2033: A comparison of an F64 with a float literal zero
The code compares an F64 with a float literal zero.
Why Una rejects it
== on F64 is identity, and < is IEEE totalOrder (spec/29, ADR 0114): -0.0 and 0.0 are two values, and -0.0 < 0.0. So x == 0.0 is false and x < 0.0 is true when x is -0.0, which ordinary arithmetic produces (0.0 * -5.0). Every mainstream language says the opposite.
The checker refuses every comparison of an F64 with a literal zero, 0.0 or -0.0, in code and in clauses: ==, !=, is, a when arm, <, <=, > and >=.
A clamp written with the prelude's min and max maps NaN to its upper bound; x.clamp(lo: a, hi: b) keeps NaN. Under totalOrder, NaN is the greatest F64, so x > c and x >= c are true for NaN for every literal c, and contains(xs, item: 0.0) finds only the positive zero.
How to fix it
The repairs say which test is meant:
- for an equality,
x.is_zero()accepts both zeros, andx.is_positive_zero()orx.is_negative_zero()one of them; - for an ordering, the IEEE methods
x.ieee_lt(other: 0.0),x.ieee_le(other: 0.0),x.ieee_gt(other: 0.0)andx.ieee_ge(other: 0.0)treat the zeros as equal;x.is_sign_negative()tests the sign bit.
The repair at a glance
module demo
- pub safe_ratio(n: F64, d: F64) -> F64 = if d == 0.0 then 0.0 else n / d
+ pub safe_ratio(n: F64, d: F64) -> F64 = if d.is_zero() then 0.0 else n / dFailing example
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"module demo
pub safe_ratio(n: F64, d: F64) -> F64 = if d == 0.0 then 0.0 else n / dFixed example
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"module demo
pub safe_ratio(n: F64, d: F64) -> F64 = if d.is_zero() then 0.0 else n / d