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 // pretty-expanded FIXME #23616
15 use std::num::Float as StdFloat;
17 pub static FUZZY_EPSILON: f64 = 0.1;
19 pub trait FuzzyEq<Eps> {
20 fn fuzzy_eq(&self, other: &Self) -> bool;
21 fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool;
24 trait Float: FuzzyEq<Self> {
28 impl FuzzyEq<f32> for f32 {
29 fn fuzzy_eq(&self, other: &f32) -> bool {
30 self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32))
33 fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool {
34 (*self - *other).abs() < *epsilon
39 fn two_pi() -> f32 { 6.28318530717958647692528676655900576_f32 }
42 impl FuzzyEq<f64> for f64 {
43 fn fuzzy_eq(&self, other: &f64) -> bool {
44 self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64))
47 fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool {
48 (*self - *other).abs() < *epsilon
53 fn two_pi() -> f64 { 6.28318530717958647692528676655900576_f64 }
56 fn compare<F:Float>(f1: F) -> bool {
57 let f2 = Float::two_pi();
62 assert!(compare::<f32>(6.28318530717958647692528676655900576));
63 assert!(compare::<f32>(6.29));
64 assert!(compare::<f32>(6.3));
65 assert!(compare::<f32>(6.19));
66 assert!(!compare::<f32>(7.28318530717958647692528676655900576));
67 assert!(!compare::<f32>(6.18));
69 assert!(compare::<f64>(6.28318530717958647692528676655900576));
70 assert!(compare::<f64>(6.29));
71 assert!(compare::<f64>(6.3));
72 assert!(compare::<f64>(6.19));
73 assert!(!compare::<f64>(7.28318530717958647692528676655900576));
74 assert!(!compare::<f64>(6.18));