]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
970d7e83 LB |
11 | // Test for issue #4183: use of Self in supertraits. |
12 | ||
1a4d82fc | 13 | pub static FUZZY_EPSILON: f64 = 0.1; |
970d7e83 LB |
14 | |
15 | pub trait FuzzyEq<Eps> { | |
16 | fn fuzzy_eq(&self, other: &Self) -> bool; | |
17 | fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool; | |
18 | } | |
19 | ||
20 | trait Float: FuzzyEq<Self> { | |
21 | fn two_pi() -> Self; | |
22 | } | |
23 | ||
24 | impl FuzzyEq<f32> for f32 { | |
25 | fn fuzzy_eq(&self, other: &f32) -> bool { | |
26 | self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32)) | |
27 | } | |
28 | ||
29 | fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool { | |
1a4d82fc | 30 | (*self - *other).abs() < *epsilon |
970d7e83 LB |
31 | } |
32 | } | |
33 | ||
34 | impl Float for f32 { | |
35 | fn two_pi() -> f32 { 6.28318530717958647692528676655900576_f32 } | |
36 | } | |
37 | ||
38 | impl FuzzyEq<f64> for f64 { | |
39 | fn fuzzy_eq(&self, other: &f64) -> bool { | |
40 | self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64)) | |
41 | } | |
42 | ||
43 | fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool { | |
1a4d82fc | 44 | (*self - *other).abs() < *epsilon |
970d7e83 LB |
45 | } |
46 | } | |
47 | ||
48 | impl Float for f64 { | |
49 | fn two_pi() -> f64 { 6.28318530717958647692528676655900576_f64 } | |
50 | } | |
51 | ||
52 | fn compare<F:Float>(f1: F) -> bool { | |
53 | let f2 = Float::two_pi(); | |
54 | f1.fuzzy_eq(&f2) | |
55 | } | |
56 | ||
57 | pub fn main() { | |
58 | assert!(compare::<f32>(6.28318530717958647692528676655900576)); | |
59 | assert!(compare::<f32>(6.29)); | |
60 | assert!(compare::<f32>(6.3)); | |
61 | assert!(compare::<f32>(6.19)); | |
62 | assert!(!compare::<f32>(7.28318530717958647692528676655900576)); | |
63 | assert!(!compare::<f32>(6.18)); | |
64 | ||
65 | assert!(compare::<f64>(6.28318530717958647692528676655900576)); | |
66 | assert!(compare::<f64>(6.29)); | |
67 | assert!(compare::<f64>(6.3)); | |
68 | assert!(compare::<f64>(6.19)); | |
69 | assert!(!compare::<f64>(7.28318530717958647692528676655900576)); | |
70 | assert!(!compare::<f64>(6.18)); | |
71 | } |