2012-08-30 14:44:28 -07:00
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Additional general-purpose comparison functionality.
|
2012-06-07 18:12:50 -07:00
|
|
|
|
|
|
|
const fuzzy_epsilon: float = 1.0e-6;
|
|
|
|
|
2012-08-11 10:08:42 -04:00
|
|
|
trait FuzzyEq {
|
2012-08-30 14:44:28 -07:00
|
|
|
pure fn fuzzy_eq(other: &self) -> bool;
|
2012-06-07 18:12:50 -07:00
|
|
|
}
|
|
|
|
|
2012-08-11 10:08:42 -04:00
|
|
|
impl float: FuzzyEq {
|
2012-08-30 14:44:28 -07:00
|
|
|
pure fn fuzzy_eq(other: &float) -> bool {
|
|
|
|
return float::abs(self - *other) < fuzzy_epsilon;
|
2012-06-07 18:12:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-11 10:08:42 -04:00
|
|
|
impl f32: FuzzyEq {
|
2012-08-30 14:44:28 -07:00
|
|
|
pure fn fuzzy_eq(other: &f32) -> bool {
|
|
|
|
return f32::abs(self - *other) < (fuzzy_epsilon as f32);
|
2012-06-07 18:12:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-11 10:08:42 -04:00
|
|
|
impl f64: FuzzyEq {
|
2012-08-30 14:44:28 -07:00
|
|
|
pure fn fuzzy_eq(other: &f64) -> bool {
|
|
|
|
return f64::abs(self - *other) < (fuzzy_epsilon as f64);
|
2012-06-07 18:12:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fuzzy_equals() {
|
2012-08-30 14:44:28 -07:00
|
|
|
assert ((&1.0).fuzzy_eq(&1.0));
|
|
|
|
assert ((&1.0f32).fuzzy_eq(&1.0f32));
|
|
|
|
assert ((&1.0f64).fuzzy_eq(&1.0f64));
|
2012-06-07 18:12:50 -07:00
|
|
|
}
|
|
|
|
|