]> git.lizzy.rs Git - rust.git/blob - src/docs/suboptimal_flops.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / suboptimal_flops.txt
1 ### What it does
2 Looks for floating-point expressions that
3 can be expressed using built-in methods to improve both
4 accuracy and performance.
5
6 ### Why is this bad?
7 Negatively impacts accuracy and performance.
8
9 ### Example
10 ```
11 use std::f32::consts::E;
12
13 let a = 3f32;
14 let _ = (2f32).powf(a);
15 let _ = E.powf(a);
16 let _ = a.powf(1.0 / 2.0);
17 let _ = a.log(2.0);
18 let _ = a.log(10.0);
19 let _ = a.log(E);
20 let _ = a.powf(2.0);
21 let _ = a * 2.0 + 4.0;
22 let _ = if a < 0.0 {
23     -a
24 } else {
25     a
26 };
27 let _ = if a < 0.0 {
28     a
29 } else {
30     -a
31 };
32 ```
33
34 is better expressed as
35
36 ```
37 use std::f32::consts::E;
38
39 let a = 3f32;
40 let _ = a.exp2();
41 let _ = a.exp();
42 let _ = a.sqrt();
43 let _ = a.log2();
44 let _ = a.log10();
45 let _ = a.ln();
46 let _ = a.powi(2);
47 let _ = a.mul_add(2.0, 4.0);
48 let _ = a.abs();
49 let _ = -a.abs();
50 ```