mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
a067cd24ac
Using `f32::EPSILON` or `f64::EPSILON` as the floating-point equality comparison error margin is incorrect, yet `float_cmp` has until now recommended this be done. This change fixes the given guidance (both in docs and compiler hints) to not reference these unsuitable constants. Instead, the guidance now clarifies that the scenarios in which an absolute error margin is usable, provides a reference implementation of using a user-defined absolute error margin (as an absolute error margin can only be used-defined and may be different for different comparisons) and references the floating point guide for a reference implementation of relative error based equaltiy comparison for when absolute error margin cannot be used. changelog: Fix guidance of [`float_cmp`] and [`float_cmp_const`] to not incorrectly recommend `f64::EPSILON` as the error margin. Fixes #6816
53 lines
2 KiB
Text
53 lines
2 KiB
Text
error: strict comparison of `f32` or `f64` constant
|
|
--> tests/ui/float_cmp_const.rs:16:5
|
|
|
|
|
LL | 1f32 == ONE;
|
|
| ^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1f32 - ONE).abs() < error_margin`
|
|
|
|
|
= note: `-D clippy::float-cmp-const` implied by `-D warnings`
|
|
= help: to override `-D warnings` add `#[allow(clippy::float_cmp_const)]`
|
|
|
|
error: strict comparison of `f32` or `f64` constant
|
|
--> tests/ui/float_cmp_const.rs:18:5
|
|
|
|
|
LL | TWO == ONE;
|
|
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(TWO - ONE).abs() < error_margin`
|
|
|
|
error: strict comparison of `f32` or `f64` constant
|
|
--> tests/ui/float_cmp_const.rs:20:5
|
|
|
|
|
LL | TWO != ONE;
|
|
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(TWO - ONE).abs() > error_margin`
|
|
|
|
error: strict comparison of `f32` or `f64` constant
|
|
--> tests/ui/float_cmp_const.rs:22:5
|
|
|
|
|
LL | ONE + ONE == TWO;
|
|
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE + ONE - TWO).abs() < error_margin`
|
|
|
|
error: strict comparison of `f32` or `f64` constant
|
|
--> tests/ui/float_cmp_const.rs:25:5
|
|
|
|
|
LL | x as f32 == ONE;
|
|
| ^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x as f32 - ONE).abs() < error_margin`
|
|
|
|
error: strict comparison of `f32` or `f64` constant
|
|
--> tests/ui/float_cmp_const.rs:29:5
|
|
|
|
|
LL | v == ONE;
|
|
| ^^^^^^^^ help: consider comparing them within some margin of error: `(v - ONE).abs() < error_margin`
|
|
|
|
error: strict comparison of `f32` or `f64` constant
|
|
--> tests/ui/float_cmp_const.rs:31:5
|
|
|
|
|
LL | v != ONE;
|
|
| ^^^^^^^^ help: consider comparing them within some margin of error: `(v - ONE).abs() > error_margin`
|
|
|
|
error: strict comparison of `f32` or `f64` constant arrays
|
|
--> tests/ui/float_cmp_const.rs:64:5
|
|
|
|
|
LL | NON_ZERO_ARRAY == NON_ZERO_ARRAY2;
|
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
error: aborting due to 8 previous errors
|
|
|