]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/float_cmp.txt
Rollup merge of #101389 - lukaslueg:rcgetmutdocs, r=m-ou-se
[rust.git] / src / tools / clippy / src / docs / float_cmp.txt
1 ### What it does
2 Checks for (in-)equality comparisons on floating-point
3 values (apart from zero), except in functions called `*eq*` (which probably
4 implement equality for a type involving floats).
5
6 ### Why is this bad?
7 Floating point calculations are usually imprecise, so
8 asking if two values are *exactly* equal is asking for trouble. For a good
9 guide on what to do, see [the floating point
10 guide](http://www.floating-point-gui.de/errors/comparison).
11
12 ### Example
13 ```
14 let x = 1.2331f64;
15 let y = 1.2332f64;
16
17 if y == 1.23f64 { }
18 if y != x {} // where both are floats
19 ```
20
21 Use instead:
22 ```
23 let error_margin = f64::EPSILON; // Use an epsilon for comparison
24 // Or, if Rust <= 1.42, use `std::f64::EPSILON` constant instead.
25 // let error_margin = std::f64::EPSILON;
26 if (y - 1.23f64).abs() < error_margin { }
27 if (y - x).abs() > error_margin { }
28 ```