]> git.lizzy.rs Git - rust.git/blob - src/docs/float_equality_without_abs.txt
556b574e15d303a4b8c6774b1f9e94ec6b3c89dd
[rust.git] / src / docs / float_equality_without_abs.txt
1 ### What it does
2 Checks for statements of the form `(a - b) < f32::EPSILON` or
3 `(a - b) < f64::EPSILON`. Notes the missing `.abs()`.
4
5 ### Why is this bad?
6 The code without `.abs()` is more likely to have a bug.
7
8 ### Known problems
9 If the user can ensure that b is larger than a, the `.abs()` is
10 technically unnecessary. However, it will make the code more robust and doesn't have any
11 large performance implications. If the abs call was deliberately left out for performance
12 reasons, it is probably better to state this explicitly in the code, which then can be done
13 with an allow.
14
15 ### Example
16 ```
17 pub fn is_roughly_equal(a: f32, b: f32) -> bool {
18     (a - b) < f32::EPSILON
19 }
20 ```
21 Use instead:
22 ```
23 pub fn is_roughly_equal(a: f32, b: f32) -> bool {
24     (a - b).abs() < f32::EPSILON
25 }
26 ```