Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::float_cmp: strict comparison of `f32` or `f64`
What it meansChecks for (in-)equality comparisons on floating-point values (apart from zero), except in functions called *eq* (which probably implement equality for a type involving floats). It is in the pedantic group and off by default.
What you see
warning: strict comparison of `f32` or `f64` --> main.rs:11:17 | 11 | let are_equal = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#float_cmp = note: `-W clippy::float-cmp` implied by `-W clippy::pedantic`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Floating point calculations are usually imprecise, so asking if two values are exactly equal is asking for trouble because arriving at the same logical result via different routes (e.g. calculation versus constant) may yield different values.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let a: f64 = 1000.1;
let b: f64 = 0.2;
let x = a + b;
let y = 1000.3; // Expected value.
// Actual value: 1000.3000000000001
println!("{x}");
let are_equal = x == y;
println!("{are_equal}"); // false
Write this instead
let a: f64 = 1000.1;
let b: f64 = 0.2;
let x = a + b;
let y = 1000.3; // Expected value.
const ALLOWED_ERROR_VEHICLE_LENGTH_CM: f64 = 0.1;
let within_tolerance = (x - y).abs() < ALLOWED_ERROR_VEHICLE_LENGTH_CM;
println!("{within_tolerance}"); // true
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
float_cmp = "warn"In code
#![warn(clippy::float_cmp)]One run
cargo clippy -- -W clippy::float-cmpThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for (in-)equality comparisons on floating-point
values (apart from zero), except in functions called *eq* (which probably
implement equality for a type involving floats).
Why is this bad?
Floating point calculations are usually imprecise, so asking if two values are exactly equal is asking for trouble because arriving at the same logical result via different routes (e.g. calculation versus constant) may yield different values.
Example
let a: f64 = 1000.1;
let b: f64 = 0.2;
let x = a + b;
let y = 1000.3; // Expected value.
// Actual value: 1000.3000000000001
println!("{x}");
let are_equal = x == y;
println!("{are_equal}"); // false
The correct way to compare floating point numbers is to define an allowed error margin. This may be challenging if there is no “natural” error margin to permit. Broadly speaking, there are two cases:
- If your values are in a known range and you can define a threshold for “close enough to be equal”, it may be appropriate to define an absolute error margin. For example, if your data is “length of vehicle in centimeters”, you may consider 0.1 cm to be “close enough”.
- If your code is more general and you do not know the range of values, you should use a relative error margin, accepting e.g. 0.1% of error regardless of specific values.
For the scenario where you can define a meaningful absolute error margin, consider using:
let a: f64 = 1000.1;
let b: f64 = 0.2;
let x = a + b;
let y = 1000.3; // Expected value.
const ALLOWED_ERROR_VEHICLE_LENGTH_CM: f64 = 0.1;
let within_tolerance = (x - y).abs() < ALLOWED_ERROR_VEHICLE_LENGTH_CM;
println!("{within_tolerance}"); // true
NOTE: Do not use f64::EPSILON - while the error margin is often called “epsilon”, this is
a different use of the term that is not suitable for floating point equality comparison.
Indeed, for the example above using f64::EPSILON as the allowed error would return false.
For the scenario where no meaningful absolute error can be defined, refer to
the floating point guide
for a reference implementation of relative error based comparison of floating point values.
MIN_NORMAL in the reference implementation is equivalent to MIN_POSITIVE in Rust.
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- explicit_iter_loopChecks for loops on x.iter() where &x will do, and suggests the latter
- filter_map_nextcalled `filter_map(..).next()` on an `Iterator`
- flat_map_optionused `flat_map` where `filter_map` could be used instead
- fn_params_excessive_boolsChecks for excessive use of bools in function declarations
- format_collectuse of `format!` to build up a string from an iterator
- format_push_string`format!(..)` appended to existing `String`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?