2024-06-18 17:34:49 +00:00
|
|
|
// FIXME(f16_f128): const casting is not yet supported for these types. Add when available.
|
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#![warn(clippy::float_cmp)]
|
2020-03-20 10:42:39 +00:00
|
|
|
#![allow(
|
|
|
|
unused,
|
|
|
|
clippy::no_effect,
|
2020-10-23 20:16:59 +00:00
|
|
|
clippy::op_ref,
|
2020-03-20 10:42:39 +00:00
|
|
|
clippy::unnecessary_operation,
|
2021-09-28 17:03:12 +00:00
|
|
|
clippy::cast_lossless
|
2020-03-20 10:42:39 +00:00
|
|
|
)]
|
2023-08-24 19:32:12 +00:00
|
|
|
//@no-rustfix
|
2015-05-06 08:01:49 +00:00
|
|
|
use std::ops::Add;
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
const ZERO: f32 = 0.0;
|
|
|
|
const ONE: f32 = ZERO + 1.0;
|
2015-05-06 08:01:49 +00:00
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn twice<T>(x: T) -> T
|
|
|
|
where
|
2019-07-27 21:53:26 +00:00
|
|
|
T: Add<T, Output = T> + Copy,
|
2018-12-09 22:26:16 +00:00
|
|
|
{
|
2015-08-11 18:22:20 +00:00
|
|
|
x + x
|
2015-05-06 08:01:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 08:30:11 +00:00
|
|
|
fn eq_fl(x: f32, y: f32) -> bool {
|
2021-03-12 14:30:50 +00:00
|
|
|
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
|
2015-09-02 08:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fl_eq(x: f32, y: f32) -> bool {
|
2021-03-12 14:30:50 +00:00
|
|
|
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
|
2015-09-02 08:30:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
struct X {
|
|
|
|
val: f32,
|
|
|
|
}
|
2015-09-02 08:30:11 +00:00
|
|
|
|
|
|
|
impl PartialEq for X {
|
|
|
|
fn eq(&self, o: &X) -> bool {
|
|
|
|
if self.val.is_nan() {
|
|
|
|
o.val.is_nan()
|
|
|
|
} else {
|
|
|
|
self.val == o.val // no error, inside "eq" fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 16:13:53 +00:00
|
|
|
impl PartialEq<f32> for X {
|
|
|
|
fn eq(&self, o: &f32) -> bool {
|
|
|
|
if self.val.is_nan() {
|
|
|
|
o.is_nan()
|
|
|
|
} else {
|
|
|
|
self.val == *o // no error, inside "eq" fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 08:01:49 +00:00
|
|
|
fn main() {
|
2015-08-17 10:06:56 +00:00
|
|
|
ZERO == 0f32; //no error, comparison with zero is ok
|
2020-04-07 22:04:33 +00:00
|
|
|
1.0f32 != f32::INFINITY; // also comparison with infinity
|
|
|
|
1.0f32 != f32::NEG_INFINITY; // and negative infinity
|
2015-08-17 10:06:56 +00:00
|
|
|
ZERO == 0.0; //no error, comparison with zero is ok
|
2015-11-10 10:19:33 +00:00
|
|
|
ZERO + ZERO != 1.0; //no error, comparison with zero is ok
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2023-09-12 16:13:53 +00:00
|
|
|
let x = X { val: 1.0 };
|
|
|
|
x == 1.0; // no error, custom type that implement PartialOrder for float is not checked
|
|
|
|
|
2016-06-29 17:47:51 +00:00
|
|
|
ONE == 1f32;
|
2016-06-29 19:25:23 +00:00
|
|
|
ONE == 1.0 + 0.0;
|
|
|
|
ONE + ONE == ZERO + ONE + ONE;
|
2016-06-29 17:47:51 +00:00
|
|
|
ONE != 2.0;
|
2015-08-17 10:06:56 +00:00
|
|
|
ONE != 0.0; // no error, comparison with zero is ok
|
2016-06-29 17:47:51 +00:00
|
|
|
twice(ONE) != ONE;
|
|
|
|
ONE as f64 != 2.0;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64`
|
2015-08-17 10:06:56 +00:00
|
|
|
ONE as f64 != 0.0; // no error, comparison with zero is ok
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
let x: f64 = 1.0;
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2016-06-29 17:47:51 +00:00
|
|
|
x == 1.0;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64`
|
2015-08-17 10:06:56 +00:00
|
|
|
x != 0f64; // no error, comparison with zero is ok
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2016-06-29 17:47:51 +00:00
|
|
|
twice(x) != twice(ONE as f64);
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64`
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-08-17 10:06:56 +00:00
|
|
|
x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
|
2015-08-11 18:22:20 +00:00
|
|
|
x > 0.0;
|
|
|
|
x <= 0.0;
|
|
|
|
x >= 0.0;
|
2016-06-25 16:59:37 +00:00
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
let xs: [f32; 1] = [0.0];
|
2016-06-25 16:59:37 +00:00
|
|
|
let a: *const f32 = xs.as_ptr();
|
|
|
|
let b: *const f32 = xs.as_ptr();
|
|
|
|
|
2017-02-17 08:32:51 +00:00
|
|
|
assert_eq!(a, b); // no errors
|
2019-07-15 17:46:58 +00:00
|
|
|
|
2020-03-20 10:42:39 +00:00
|
|
|
const ZERO_ARRAY: [f32; 2] = [0.0, 0.0];
|
|
|
|
const NON_ZERO_ARRAY: [f32; 2] = [0.0, 0.1];
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
let j = 1;
|
|
|
|
|
|
|
|
ZERO_ARRAY[i] == NON_ZERO_ARRAY[j]; // ok, because lhs is zero regardless of i
|
|
|
|
NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64`
|
2020-03-20 10:42:39 +00:00
|
|
|
|
2019-09-19 12:57:43 +00:00
|
|
|
let a1: [f32; 1] = [0.0];
|
|
|
|
let a2: [f32; 1] = [1.1];
|
|
|
|
|
2020-03-20 10:42:39 +00:00
|
|
|
a1 == a2;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` arrays
|
2020-03-20 10:42:39 +00:00
|
|
|
a1[0] == a2[0];
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64`
|
2019-09-19 12:57:43 +00:00
|
|
|
|
2019-07-15 17:46:58 +00:00
|
|
|
// no errors - comparing signums is ok
|
|
|
|
let x32 = 3.21f32;
|
|
|
|
1.23f32.signum() == x32.signum();
|
|
|
|
1.23f32.signum() == -(x32.signum());
|
|
|
|
1.23f32.signum() == 3.21f32.signum();
|
|
|
|
|
|
|
|
1.23f32.signum() != x32.signum();
|
|
|
|
1.23f32.signum() != -(x32.signum());
|
|
|
|
1.23f32.signum() != 3.21f32.signum();
|
|
|
|
|
|
|
|
let x64 = 3.21f64;
|
|
|
|
1.23f64.signum() == x64.signum();
|
|
|
|
1.23f64.signum() == -(x64.signum());
|
|
|
|
1.23f64.signum() == 3.21f64.signum();
|
|
|
|
|
|
|
|
1.23f64.signum() != x64.signum();
|
|
|
|
1.23f64.signum() != -(x64.signum());
|
|
|
|
1.23f64.signum() != 3.21f64.signum();
|
2020-10-23 20:16:59 +00:00
|
|
|
|
|
|
|
// the comparison should also look through references
|
|
|
|
&0.0 == &ZERO;
|
|
|
|
&&&&0.0 == &&&&ZERO;
|
2015-05-06 08:01:49 +00:00
|
|
|
}
|