← All lints

float_equality

L0301Correctnesswarn

Exact float equality comparison

== or != on floats

Flagged
let a: F64 = 0.1 + 0.2
print(a == 0.3)
Write instead
let a: F64 = 0.1 + 0.2
print(yes(abs(a - 0.3) < 1e-9))

Why

Comparing F64 values with == or != is fragile under IEEE-754 rounding. Compare abs(a - b) < eps instead.

The practice it serves

Code that is wrong or almost certainly a bug. Denied by cpl lint. Family: K. Arithmetic, overflow, modulus and floats.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
let a: F64 = 0.1 + 0.2
print(yes(a < 0.5))
fn half(x: F64) -> F64 = x / 2.0
fn count(xs: List<Int>, v: Int) -> Int = xs.count(x => x == v)
print(count([1, 2, 2], 2))
fn exp(xs: List<Int>) -> List<Int> = [x * 2 for x in xs]
print(yes(exp([1]) == [2]))

Turn it off

For one block, wrap it in with lint(float_equality: allow):. For a project, add float_equality = "allow" under [lints] in cp.toml.

Generated from spec/lints/float_equality, the same record the linter is built from.