Home / Docs / Diagnostics

U2033: A comparison of an F64 with a float literal zero

Release v0.4.0

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:

The repair at a glance

src/demo.unadiff
  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 / d

Failing example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"
src/demo.unauna
module demo

pub safe_ratio(n: F64, d: F64) -> F64 = if d == 0.0 then 0.0 else n / d

Fixed example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"
src/demo.unauna
module demo

pub safe_ratio(n: F64, d: F64) -> F64 = if d.is_zero() then 0.0 else n / d