2020-08-28 14:10:16 +00:00
|
|
|
#![warn(clippy::float_equality_without_abs)]
|
2023-08-24 19:32:12 +00:00
|
|
|
//@no-rustfix
|
2020-08-28 14:10:16 +00:00
|
|
|
pub fn is_roughly_equal(a: f32, b: f32) -> bool {
|
|
|
|
(a - b) < f32::EPSILON
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
|
|
|
//~| NOTE: `-D clippy::float-equality-without-abs` implied by `-D warnings`
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
// all errors
|
|
|
|
is_roughly_equal(1.0, 2.0);
|
|
|
|
let a = 0.05;
|
|
|
|
let b = 0.0500001;
|
|
|
|
|
|
|
|
let _ = (a - b) < f32::EPSILON;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = a - b < f32::EPSILON;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = a - b.abs() < f32::EPSILON;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = (a as f64 - b as f64) < f64::EPSILON;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = 1.0 - 2.0 < f32::EPSILON;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
|
|
|
|
let _ = f32::EPSILON > (a - b);
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = f32::EPSILON > a - b;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = f32::EPSILON > a - b.abs();
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = f64::EPSILON > (a as f64 - b as f64);
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
let _ = f32::EPSILON > 1.0 - 2.0;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: float equality check without `.abs()`
|
2020-08-28 14:10:16 +00:00
|
|
|
|
|
|
|
// those are correct
|
|
|
|
let _ = (a - b).abs() < f32::EPSILON;
|
|
|
|
let _ = (a as f64 - b as f64).abs() < f64::EPSILON;
|
|
|
|
|
|
|
|
let _ = f32::EPSILON > (a - b).abs();
|
|
|
|
let _ = f64::EPSILON > (a as f64 - b as f64).abs();
|
|
|
|
}
|