2013-01-29 15:48:04 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the
|
|
|
|
// COPYRIGHT file at the top-level directory of this distribution and at
|
2012-12-03 18:48:01 -06:00
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-08-30 16:44:28 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2013-01-28 18:28:23 -06:00
|
|
|
//! Additional general-purpose comparison functionality.
|
2012-06-07 20:12:50 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::f32;
|
|
|
|
use core::f64;
|
|
|
|
use core::float;
|
|
|
|
|
2012-06-07 20:12:50 -05:00
|
|
|
const fuzzy_epsilon: float = 1.0e-6;
|
|
|
|
|
2012-09-27 18:43:15 -05:00
|
|
|
pub trait FuzzyEq {
|
2013-01-28 18:28:40 -06:00
|
|
|
pure fn fuzzy_eq(&self, other: &self) -> bool;
|
2013-01-28 20:12:25 -06:00
|
|
|
pure fn fuzzy_eq_eps(&self, other: &self, epsilon: &self) -> bool;
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
impl float: FuzzyEq {
|
2013-01-28 18:28:40 -06:00
|
|
|
pure fn fuzzy_eq(&self, other: &float) -> bool {
|
2013-01-29 15:54:55 -06:00
|
|
|
self.fuzzy_eq_eps(other, &fuzzy_epsilon)
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
2013-01-28 20:12:25 -06:00
|
|
|
|
|
|
|
pure fn fuzzy_eq_eps(&self, other: &float, epsilon: &float) -> bool {
|
|
|
|
float::abs(*self - *other) < *epsilon
|
|
|
|
}
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
impl f32: FuzzyEq {
|
2013-01-28 18:28:40 -06:00
|
|
|
pure fn fuzzy_eq(&self, other: &f32) -> bool {
|
2013-01-29 15:54:55 -06:00
|
|
|
self.fuzzy_eq_eps(other, &(fuzzy_epsilon as f32))
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
2013-01-28 20:12:25 -06:00
|
|
|
|
|
|
|
pure fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool {
|
|
|
|
f32::abs(*self - *other) < *epsilon
|
|
|
|
}
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
impl f64: FuzzyEq {
|
2013-01-28 18:28:40 -06:00
|
|
|
pure fn fuzzy_eq(&self, other: &f64) -> bool {
|
2013-01-29 15:54:55 -06:00
|
|
|
self.fuzzy_eq_eps(other, &(fuzzy_epsilon as f64))
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
2013-01-28 20:12:25 -06:00
|
|
|
|
|
|
|
pure fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool {
|
|
|
|
f64::abs(*self - *other) < *epsilon
|
|
|
|
}
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fuzzy_equals() {
|
2013-01-30 18:21:10 -06:00
|
|
|
assert (&1.0f).fuzzy_eq(&1.0);
|
2013-01-28 18:28:40 -06:00
|
|
|
assert (&1.0f32).fuzzy_eq(&1.0f32);
|
|
|
|
assert (&1.0f64).fuzzy_eq(&1.0f64);
|
2012-06-07 20:12:50 -05:00
|
|
|
}
|
2013-01-28 20:12:25 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fuzzy_eq_eps() {
|
2013-01-30 18:21:10 -06:00
|
|
|
assert (&1.2f).fuzzy_eq_eps(&0.9, &0.5);
|
|
|
|
assert !(&1.5f).fuzzy_eq_eps(&0.9, &0.5);
|
2013-01-28 20:12:25 -06:00
|
|
|
}
|