summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/float_equality_without_abs.txt
blob: 556b574e15d303a4b8c6774b1f9e94ec6b3c89dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
### What it does
Checks for statements of the form `(a - b) < f32::EPSILON` or
`(a - b) < f64::EPSILON`. Notes the missing `.abs()`.

### Why is this bad?
The code without `.abs()` is more likely to have a bug.

### Known problems
If the user can ensure that b is larger than a, the `.abs()` is
technically unnecessary. However, it will make the code more robust and doesn't have any
large performance implications. If the abs call was deliberately left out for performance
reasons, it is probably better to state this explicitly in the code, which then can be done
with an allow.

### Example
```
pub fn is_roughly_equal(a: f32, b: f32) -> bool {
    (a - b) < f32::EPSILON
}
```
Use instead:
```
pub fn is_roughly_equal(a: f32, b: f32) -> bool {
    (a - b).abs() < f32::EPSILON
}
```