]> git.lizzy.rs Git - rust.git/blob - tests/ui/float_cmp.rs
Merge branch 'master' into no_effect_with_drop
[rust.git] / tests / ui / float_cmp.rs
1
2
3
4 #![warn(float_cmp)]
5 #![allow(unused, no_effect, unnecessary_operation, cast_lossless)]
6
7 use std::ops::Add;
8
9 const ZERO : f32 = 0.0;
10 const ONE : f32 = ZERO + 1.0;
11
12 fn twice<T>(x : T) -> T where T : Add<T, Output = T>, T : Copy {
13     x + x
14 }
15
16 fn eq_fl(x: f32, y: f32) -> bool {
17     if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
18 }
19
20 fn fl_eq(x: f32, y: f32) -> bool {
21     if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
22 }
23
24 struct X { val: f32 }
25
26 impl PartialEq for X {
27     fn eq(&self, o: &X) -> bool {
28         if self.val.is_nan() {
29             o.val.is_nan()
30         } else {
31             self.val == o.val // no error, inside "eq" fn
32         }
33     }
34 }
35
36 fn main() {
37     ZERO == 0f32; //no error, comparison with zero is ok
38     1.0f32 != ::std::f32::INFINITY; // also comparison with infinity
39     1.0f32 != ::std::f32::NEG_INFINITY; // and negative infinity
40     ZERO == 0.0; //no error, comparison with zero is ok
41     ZERO + ZERO != 1.0; //no error, comparison with zero is ok
42
43     ONE == 1f32;
44     ONE == 1.0 + 0.0;
45     ONE + ONE == ZERO + ONE + ONE;
46     ONE != 2.0;
47     ONE != 0.0; // no error, comparison with zero is ok
48     twice(ONE) != ONE;
49     ONE as f64 != 2.0;
50     ONE as f64 != 0.0; // no error, comparison with zero is ok
51
52     let x : f64 = 1.0;
53
54     x == 1.0;
55     x != 0f64; // no error, comparison with zero is ok
56
57     twice(x) != twice(ONE as f64);
58
59     x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
60     x > 0.0;
61     x <= 0.0;
62     x >= 0.0;
63
64     let xs : [f32; 1] = [0.0];
65     let a: *const f32 = xs.as_ptr();
66     let b: *const f32 = xs.as_ptr();
67
68     assert_eq!(a, b); // no errors
69 }