1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -19,24 +19,37 @@ const fuzzy_epsilon: float = 1.0e-6;
19
19
20
20
pub trait FuzzyEq {
21
21
pure fn fuzzy_eq ( & self , other: & self ) -> bool ;
22
+ pure fn fuzzy_eq_eps ( & self , other: & self , epsilon: & self ) -> bool ;
22
23
}
23
24
24
25
impl float: FuzzyEq {
25
26
pure fn fuzzy_eq ( & self , other : & float ) -> bool {
26
27
float:: abs ( * self - * other) < fuzzy_epsilon
27
28
}
29
+
30
+ pure fn fuzzy_eq_eps ( & self , other : & float , epsilon : & float ) -> bool {
31
+ float:: abs ( * self - * other) < * epsilon
32
+ }
28
33
}
29
34
30
35
impl f32 : FuzzyEq {
31
36
pure fn fuzzy_eq ( & self , other : & f32 ) -> bool {
32
37
f32:: abs ( * self - * other) < ( fuzzy_epsilon as f32 )
33
38
}
39
+
40
+ pure fn fuzzy_eq_eps ( & self , other : & f32 , epsilon : & f32 ) -> bool {
41
+ f32:: abs ( * self - * other) < * epsilon
42
+ }
34
43
}
35
44
36
45
impl f64 : FuzzyEq {
37
46
pure fn fuzzy_eq ( & self , other : & f64 ) -> bool {
38
47
f64:: abs ( * self - * other) < ( fuzzy_epsilon as f64 )
39
48
}
49
+
50
+ pure fn fuzzy_eq_eps ( & self , other : & f64 , epsilon : & f64 ) -> bool {
51
+ f64:: abs ( * self - * other) < * epsilon
52
+ }
40
53
}
41
54
42
55
#[ test]
@@ -45,3 +58,9 @@ fn test_fuzzy_equals() {
45
58
assert ( & 1.0f32 ) . fuzzy_eq ( & 1.0f32 ) ;
46
59
assert ( & 1.0f64 ) . fuzzy_eq ( & 1.0f64 ) ;
47
60
}
61
+
62
+ #[ test]
63
+ fn test_fuzzy_eq_eps ( ) {
64
+ assert ( & 1.2 ) . fuzzy_eq_eps ( & 0.9 , & 0.5 ) ;
65
+ assert ! ( & 1.5 ) . fuzzy_eq_eps ( & 0.9 , & 0.5 ) ;
66
+ }
0 commit comments