Add float cmp tests for arrays

This commit is contained in:
Marcin Serwin 2020-03-20 11:42:39 +01:00
parent d3167c63f8
commit 3c738b2286
2 changed files with 51 additions and 27 deletions

View File

@ -1,5 +1,11 @@
#![warn(clippy::float_cmp)]
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation, clippy::cast_lossless)]
#![allow(
unused,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::cast_lossless,
clippy::many_single_char_names
)]
use std::ops::Add;
@ -77,12 +83,20 @@ fn main() {
assert_eq!(a, b); // no errors
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];
let a1: [f32; 1] = [0.0];
let a2: [f32; 1] = [1.1];
assert_eq!(a1[0], a2[0]);
assert_eq!(&a1[0], &a2[0]);
a1 == a2;
a1[0] == a2[0];
// no errors - comparing signums is ok
let x32 = 3.21f32;

View File

@ -1,65 +1,75 @@
error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:59:5
--> $DIR/float_cmp.rs:65:5
|
LL | ONE as f64 != 2.0;
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(ONE as f64 - 2.0).abs() > error`
|
= note: `-D clippy::float-cmp` implied by `-D warnings`
note: `f32::EPSILON` and `f64::EPSILON` are available.
--> $DIR/float_cmp.rs:59:5
--> $DIR/float_cmp.rs:65:5
|
LL | ONE as f64 != 2.0;
| ^^^^^^^^^^^^^^^^^
error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:64:5
--> $DIR/float_cmp.rs:70:5
|
LL | x == 1.0;
| ^^^^^^^^ help: consider comparing them within some error: `(x - 1.0).abs() < error`
|
note: `f32::EPSILON` and `f64::EPSILON` are available.
--> $DIR/float_cmp.rs:64:5
--> $DIR/float_cmp.rs:70:5
|
LL | x == 1.0;
| ^^^^^^^^
error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:67:5
--> $DIR/float_cmp.rs:73:5
|
LL | twice(x) != twice(ONE as f64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(twice(x) - twice(ONE as f64)).abs() > error`
|
note: `f32::EPSILON` and `f64::EPSILON` are available.
--> $DIR/float_cmp.rs:67:5
--> $DIR/float_cmp.rs:73:5
|
LL | twice(x) != twice(ONE as f64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:83:5
--> $DIR/float_cmp.rs:93:5
|
LL | assert_eq!(a1[0], a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error`
|
note: `std::f32::EPSILON` and `std::f64::EPSILON` are available.
--> $DIR/float_cmp.rs:83:5
note: `f32::EPSILON` and `f64::EPSILON` are available.
--> $DIR/float_cmp.rs:93:5
|
LL | assert_eq!(a1[0], a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:85:5
--> $DIR/float_cmp.rs:98:5
|
LL | assert_eq!(&a1[0], &a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | a1 == a2;
| ^^^^^^^^
|
note: `std::f32::EPSILON` and `std::f64::EPSILON` are available.
--> $DIR/float_cmp.rs:85:5
note: `f32::EPSILON` and `f64::EPSILON` are available.
--> $DIR/float_cmp.rs:98:5
|
LL | assert_eq!(&a1[0], &a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
LL | a1 == a2;
| ^^^^^^^^
error: aborting due to 5 previous errors
error: strict comparison of `f32` or `f64`
--> $DIR/float_cmp.rs:99:5
|
LL | a1[0] == a2[0];
| ^^^^^^^^^^^^^^ help: consider comparing them within some error: `(a1[0] - a2[0]).abs() < error`
|
note: `f32::EPSILON` and `f64::EPSILON` are available.
--> $DIR/float_cmp.rs:99:5
|
LL | a1[0] == a2[0];
| ^^^^^^^^^^^^^^
error: aborting due to 6 previous errors