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.
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.
11 // Test for issue #4183: use of Self in supertraits.
13 pub static FUZZY_EPSILON: f64 = 0.1;
15 pub trait FuzzyEq<Eps> {
16 fn fuzzy_eq(&self, other: &Self) -> bool;
17 fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool;
20 trait Float: FuzzyEq<Self> {
24 impl FuzzyEq<f32> for f32 {
25 fn fuzzy_eq(&self, other: &f32) -> bool {
26 self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32))
29 fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool {
30 (*self - *other).abs() < *epsilon
35 fn two_pi() -> f32 { 6.28318530717958647692528676655900576_f32 }
38 impl FuzzyEq<f64> for f64 {
39 fn fuzzy_eq(&self, other: &f64) -> bool {
40 self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64))
43 fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool {
44 (*self - *other).abs() < *epsilon
49 fn two_pi() -> f64 { 6.28318530717958647692528676655900576_f64 }
52 fn compare<F:Float>(f1: F) -> bool {
53 let f2 = Float::two_pi();
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));
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));