You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: tests/ui/float_cmp.stderr
+5-16
Original file line number
Diff line number
Diff line change
@@ -4,49 +4,38 @@ error: strict comparison of `f32` or `f64`
4
4
LL | ONE as f64 != 2.0;
5
5
| ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(ONE as f64 - 2.0).abs() > error_margin`
6
6
|
7
-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
8
7
= note: `-D clippy::float-cmp` implied by `-D warnings`
9
8
= help: to override `-D warnings` add `#[allow(clippy::float_cmp)]`
10
9
11
10
error: strict comparison of `f32` or `f64`
12
-
--> tests/ui/float_cmp.rs:79:5
11
+
--> tests/ui/float_cmp.rs:78:5
13
12
|
14
13
LL | x == 1.0;
15
14
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 1.0).abs() < error_margin`
16
-
|
17
-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
18
15
19
16
error: strict comparison of `f32` or `f64`
20
-
--> tests/ui/float_cmp.rs:84:5
17
+
--> tests/ui/float_cmp.rs:82:5
21
18
|
22
19
LL | twice(x) != twice(ONE as f64);
23
20
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(twice(x) - twice(ONE as f64)).abs() > error_margin`
24
-
|
25
-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
26
21
27
22
error: strict comparison of `f32` or `f64`
28
-
--> tests/ui/float_cmp.rs:106:5
23
+
--> tests/ui/float_cmp.rs:103:5
29
24
|
30
25
LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
31
26
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(NON_ZERO_ARRAY[i] - NON_ZERO_ARRAY[j]).abs() < error_margin`
32
-
|
33
-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
34
27
35
28
error: strict comparison of `f32` or `f64` arrays
36
-
--> tests/ui/float_cmp.rs:113:5
29
+
--> tests/ui/float_cmp.rs:109:5
37
30
|
38
31
LL | a1 == a2;
39
32
| ^^^^^^^^
40
-
|
41
-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
42
33
43
34
error: strict comparison of `f32` or `f64`
44
-
--> tests/ui/float_cmp.rs:116:5
35
+
--> tests/ui/float_cmp.rs:111:5
45
36
|
46
37
LL | a1[0] == a2[0];
47
38
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(a1[0] - a2[0]).abs() < error_margin`
48
-
|
49
-
= note: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
0 commit comments