]> git.lizzy.rs Git - rust.git/blob - tests/ui/floating_point_arithmetic.rs
Consolidate the accuracy and efficiency lints
[rust.git] / tests / ui / floating_point_arithmetic.rs
1 #![allow(dead_code)]
2 #![warn(clippy::floating_point_improvements)]
3
4 const TWO: f32 = 2.0;
5 const E: f32 = std::f32::consts::E;
6
7 fn check_log_base() {
8     let x = 1f32;
9     let _ = x.log(2f32);
10     let _ = x.log(10f32);
11     let _ = x.log(std::f32::consts::E);
12     let _ = x.log(TWO);
13     let _ = x.log(E);
14
15     let x = 1f64;
16     let _ = x.log(2f64);
17     let _ = x.log(10f64);
18     let _ = x.log(std::f64::consts::E);
19 }
20
21 fn check_ln1p() {
22     let x = 1f32;
23     let _ = (1.0 + x).ln();
24     let _ = (1.0 + x * 2.0).ln();
25     let _ = (1.0 + x.powi(2)).ln();
26     let _ = (1.0 + x.powi(2) * 2.0).ln();
27     let _ = (1.0 + (std::f32::consts::E - 1.0)).ln();
28     // Cases where the lint shouldn't be applied
29     let _ = (x + 1.0).ln();
30     let _ = (1.0 + x + 2.0).ln();
31     let _ = (1.0 + x - 2.0).ln();
32
33     let x = 1f64;
34     let _ = (1.0 + x).ln();
35     let _ = (1.0 + x * 2.0).ln();
36     let _ = (1.0 + x.powi(2)).ln();
37     // Cases where the lint shouldn't be applied
38     let _ = (x + 1.0).ln();
39     let _ = (1.0 + x + 2.0).ln();
40     let _ = (1.0 + x - 2.0).ln();
41 }
42
43 fn check_powf() {
44     let x = 3f32;
45     let _ = 2f32.powf(x);
46     let _ = std::f32::consts::E.powf(x);
47     let _ = x.powf(1.0 / 2.0);
48     let _ = x.powf(1.0 / 3.0);
49
50     let x = 3f64;
51     let _ = 2f64.powf(x);
52     let _ = std::f64::consts::E.powf(x);
53     let _ = x.powf(1.0 / 2.0);
54     let _ = x.powf(1.0 / 3.0);
55 }
56
57 fn check_expm1() {
58     let x = 2f32;
59     let _ = x.exp() - 1.0;
60     let _ = x.exp() - 1.0 + 2.0;
61     // Cases where the lint shouldn't be applied
62     let _ = x.exp() - 2.0;
63     let _ = x.exp() - 1.0 * 2.0;
64
65     let x = 2f64;
66     let _ = x.exp() - 1.0;
67     let _ = x.exp() - 1.0 + 2.0;
68     // Cases where the lint shouldn't be applied
69     let _ = x.exp() - 2.0;
70     let _ = x.exp() - 1.0 * 2.0;
71 }
72
73 fn main() {}