2012-07-04 16:53:12 -05:00
|
|
|
/// Additional general-purpose comparison functionality.
|
2012-06-07 20:12:50 -05:00
|
|
|
|
|
|
|
const fuzzy_epsilon: float = 1.0e-6;
|
|
|
|
|
2012-07-26 16:42:44 -05:00
|
|
|
trait fuzzy_eq {
|
|
|
|
pure fn fuzzy_eq(&&other: self) -> bool;
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fuzzy_eq of fuzzy_eq for float {
|
2012-07-26 16:42:44 -05:00
|
|
|
pure fn fuzzy_eq(&&other: float) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return float::abs(self - other) < fuzzy_epsilon;
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fuzzy_eq of fuzzy_eq for f32 {
|
2012-07-26 16:42:44 -05:00
|
|
|
pure fn fuzzy_eq(&&other: f32) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return f32::abs(self - other) < (fuzzy_epsilon as f32);
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fuzzy_eq of fuzzy_eq for f64 {
|
2012-07-26 16:42:44 -05:00
|
|
|
pure fn fuzzy_eq(&&other: f64) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return f64::abs(self - other) < (fuzzy_epsilon as f64);
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fuzzy_equals() {
|
|
|
|
assert ((1.0).fuzzy_eq(1.0));
|
|
|
|
assert ((1.0f32).fuzzy_eq(1.0f32));
|
|
|
|
assert ((1.0f64).fuzzy_eq(1.0f64));
|
|
|
|
}
|
|
|
|
|