Skip to content

Commit

Permalink
auto merge of #6263 : bjz/rust/approx-eq, r=erickt
Browse files Browse the repository at this point in the history
Moving the trait into `core` allows it to be added to the `num::Float` trait.

I have also added and `assert_approx_eq!` macro. This is useful fo making approximate assertions on types that implement the `ApproxEq` trait.

Examples:

~~~rust
// using the default epsilon value
assert_approx_eq!(1.0000001f, 1.0f);

// using a custom epsilon value
assert_approx_eq!(1.000001f, 1.0f, 1.0e-5);

// fails with: "left: 1.00001 does not approximately equal right: 1"
assert_approx_eq!(1.00001f, 1.0f);
~~~
  • Loading branch information
bors committed May 6, 2013
2 parents 6e6a4be + 0e2242f commit b6dea9d
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 363 deletions.
7 changes: 7 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ totaleq_impl!(uint)

totaleq_impl!(char)

/// Trait for testing approximate equality
pub trait ApproxEq<Eps> {
fn approx_epsilon() -> Eps;
fn approx_eq(&self, other: &Self) -> bool;
fn approx_eq_eps(&self, other: &Self, approx_epsilon: &Eps) -> bool;
}

#[deriving(Clone, Eq)]
pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }

Expand Down
164 changes: 90 additions & 74 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ impl Eq for f32 {
fn ne(&self, other: &f32) -> bool { (*self) != (*other) }
}

#[cfg(notest)]
impl ApproxEq<f32> for f32 {
#[inline(always)]
fn approx_epsilon() -> f32 { 1.0e-6 }

#[inline(always)]
fn approx_eq(&self, other: &f32) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<f32, f32>())
}

#[inline(always)]
fn approx_eq_eps(&self, other: &f32, approx_epsilon: &f32) -> bool {
(*self - *other).abs() < *approx_epsilon
}
}

#[cfg(notest)]
impl Ord for f32 {
#[inline(always)]
Expand Down Expand Up @@ -810,15 +826,6 @@ mod tests {
use super::*;
use prelude::*;

macro_rules! assert_fuzzy_eq(
($a:expr, $b:expr) => ({
let a = $a, b = $b;
if !((a - b).abs() < 1.0e-6) {
fail!(fmt!("The values were not approximately equal. Found: %? and %?", a, b));
}
})
)

#[test]
fn test_num() {
num::test_num(10f32, 2f32);
Expand Down Expand Up @@ -848,91 +855,91 @@ mod tests {

#[test]
fn test_floor() {
assert_fuzzy_eq!(1.0f32.floor(), 1.0f32);
assert_fuzzy_eq!(1.3f32.floor(), 1.0f32);
assert_fuzzy_eq!(1.5f32.floor(), 1.0f32);
assert_fuzzy_eq!(1.7f32.floor(), 1.0f32);
assert_fuzzy_eq!(0.0f32.floor(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).floor(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).floor(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).floor(), -2.0f32);
assert_fuzzy_eq!((-1.5f32).floor(), -2.0f32);
assert_fuzzy_eq!((-1.7f32).floor(), -2.0f32);
assert_approx_eq!(1.0f32.floor(), 1.0f32);
assert_approx_eq!(1.3f32.floor(), 1.0f32);
assert_approx_eq!(1.5f32.floor(), 1.0f32);
assert_approx_eq!(1.7f32.floor(), 1.0f32);
assert_approx_eq!(0.0f32.floor(), 0.0f32);
assert_approx_eq!((-0.0f32).floor(), -0.0f32);
assert_approx_eq!((-1.0f32).floor(), -1.0f32);
assert_approx_eq!((-1.3f32).floor(), -2.0f32);
assert_approx_eq!((-1.5f32).floor(), -2.0f32);
assert_approx_eq!((-1.7f32).floor(), -2.0f32);
}

#[test]
fn test_ceil() {
assert_fuzzy_eq!(1.0f32.ceil(), 1.0f32);
assert_fuzzy_eq!(1.3f32.ceil(), 2.0f32);
assert_fuzzy_eq!(1.5f32.ceil(), 2.0f32);
assert_fuzzy_eq!(1.7f32.ceil(), 2.0f32);
assert_fuzzy_eq!(0.0f32.ceil(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).ceil(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).ceil(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).ceil(), -1.0f32);
assert_fuzzy_eq!((-1.5f32).ceil(), -1.0f32);
assert_fuzzy_eq!((-1.7f32).ceil(), -1.0f32);
assert_approx_eq!(1.0f32.ceil(), 1.0f32);
assert_approx_eq!(1.3f32.ceil(), 2.0f32);
assert_approx_eq!(1.5f32.ceil(), 2.0f32);
assert_approx_eq!(1.7f32.ceil(), 2.0f32);
assert_approx_eq!(0.0f32.ceil(), 0.0f32);
assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
}

#[test]
fn test_round() {
assert_fuzzy_eq!(1.0f32.round(), 1.0f32);
assert_fuzzy_eq!(1.3f32.round(), 1.0f32);
assert_fuzzy_eq!(1.5f32.round(), 2.0f32);
assert_fuzzy_eq!(1.7f32.round(), 2.0f32);
assert_fuzzy_eq!(0.0f32.round(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).round(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).round(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).round(), -1.0f32);
assert_fuzzy_eq!((-1.5f32).round(), -2.0f32);
assert_fuzzy_eq!((-1.7f32).round(), -2.0f32);
assert_approx_eq!(1.0f32.round(), 1.0f32);
assert_approx_eq!(1.3f32.round(), 1.0f32);
assert_approx_eq!(1.5f32.round(), 2.0f32);
assert_approx_eq!(1.7f32.round(), 2.0f32);
assert_approx_eq!(0.0f32.round(), 0.0f32);
assert_approx_eq!((-0.0f32).round(), -0.0f32);
assert_approx_eq!((-1.0f32).round(), -1.0f32);
assert_approx_eq!((-1.3f32).round(), -1.0f32);
assert_approx_eq!((-1.5f32).round(), -2.0f32);
assert_approx_eq!((-1.7f32).round(), -2.0f32);
}

#[test]
fn test_trunc() {
assert_fuzzy_eq!(1.0f32.trunc(), 1.0f32);
assert_fuzzy_eq!(1.3f32.trunc(), 1.0f32);
assert_fuzzy_eq!(1.5f32.trunc(), 1.0f32);
assert_fuzzy_eq!(1.7f32.trunc(), 1.0f32);
assert_fuzzy_eq!(0.0f32.trunc(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).trunc(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).trunc(), -1.0f32);
assert_fuzzy_eq!((-1.3f32).trunc(), -1.0f32);
assert_fuzzy_eq!((-1.5f32).trunc(), -1.0f32);
assert_fuzzy_eq!((-1.7f32).trunc(), -1.0f32);
assert_approx_eq!(1.0f32.trunc(), 1.0f32);
assert_approx_eq!(1.3f32.trunc(), 1.0f32);
assert_approx_eq!(1.5f32.trunc(), 1.0f32);
assert_approx_eq!(1.7f32.trunc(), 1.0f32);
assert_approx_eq!(0.0f32.trunc(), 0.0f32);
assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
}

#[test]
fn test_fract() {
assert_fuzzy_eq!(1.0f32.fract(), 0.0f32);
assert_fuzzy_eq!(1.3f32.fract(), 0.3f32);
assert_fuzzy_eq!(1.5f32.fract(), 0.5f32);
assert_fuzzy_eq!(1.7f32.fract(), 0.7f32);
assert_fuzzy_eq!(0.0f32.fract(), 0.0f32);
assert_fuzzy_eq!((-0.0f32).fract(), -0.0f32);
assert_fuzzy_eq!((-1.0f32).fract(), -0.0f32);
assert_fuzzy_eq!((-1.3f32).fract(), -0.3f32);
assert_fuzzy_eq!((-1.5f32).fract(), -0.5f32);
assert_fuzzy_eq!((-1.7f32).fract(), -0.7f32);
assert_approx_eq!(1.0f32.fract(), 0.0f32);
assert_approx_eq!(1.3f32.fract(), 0.3f32);
assert_approx_eq!(1.5f32.fract(), 0.5f32);
assert_approx_eq!(1.7f32.fract(), 0.7f32);
assert_approx_eq!(0.0f32.fract(), 0.0f32);
assert_approx_eq!((-0.0f32).fract(), -0.0f32);
assert_approx_eq!((-1.0f32).fract(), -0.0f32);
assert_approx_eq!((-1.3f32).fract(), -0.3f32);
assert_approx_eq!((-1.5f32).fract(), -0.5f32);
assert_approx_eq!((-1.7f32).fract(), -0.7f32);
}

#[test]
fn test_real_consts() {
assert_fuzzy_eq!(Real::two_pi::<f32>(), 2f32 * Real::pi::<f32>());
assert_fuzzy_eq!(Real::frac_pi_2::<f32>(), Real::pi::<f32>() / 2f32);
assert_fuzzy_eq!(Real::frac_pi_3::<f32>(), Real::pi::<f32>() / 3f32);
assert_fuzzy_eq!(Real::frac_pi_4::<f32>(), Real::pi::<f32>() / 4f32);
assert_fuzzy_eq!(Real::frac_pi_6::<f32>(), Real::pi::<f32>() / 6f32);
assert_fuzzy_eq!(Real::frac_pi_8::<f32>(), Real::pi::<f32>() / 8f32);
assert_fuzzy_eq!(Real::frac_1_pi::<f32>(), 1f32 / Real::pi::<f32>());
assert_fuzzy_eq!(Real::frac_2_pi::<f32>(), 2f32 / Real::pi::<f32>());
assert_fuzzy_eq!(Real::frac_2_sqrtpi::<f32>(), 2f32 / Real::pi::<f32>().sqrt());
assert_fuzzy_eq!(Real::sqrt2::<f32>(), 2f32.sqrt());
assert_fuzzy_eq!(Real::frac_1_sqrt2::<f32>(), 1f32 / 2f32.sqrt());
assert_fuzzy_eq!(Real::log2_e::<f32>(), Real::e::<f32>().log2());
assert_fuzzy_eq!(Real::log10_e::<f32>(), Real::e::<f32>().log10());
assert_fuzzy_eq!(Real::log_2::<f32>(), 2f32.log());
assert_fuzzy_eq!(Real::log_10::<f32>(), 10f32.log());
assert_approx_eq!(Real::two_pi::<f32>(), 2f32 * Real::pi::<f32>());
assert_approx_eq!(Real::frac_pi_2::<f32>(), Real::pi::<f32>() / 2f32);
assert_approx_eq!(Real::frac_pi_3::<f32>(), Real::pi::<f32>() / 3f32);
assert_approx_eq!(Real::frac_pi_4::<f32>(), Real::pi::<f32>() / 4f32);
assert_approx_eq!(Real::frac_pi_6::<f32>(), Real::pi::<f32>() / 6f32);
assert_approx_eq!(Real::frac_pi_8::<f32>(), Real::pi::<f32>() / 8f32);
assert_approx_eq!(Real::frac_1_pi::<f32>(), 1f32 / Real::pi::<f32>());
assert_approx_eq!(Real::frac_2_pi::<f32>(), 2f32 / Real::pi::<f32>());
assert_approx_eq!(Real::frac_2_sqrtpi::<f32>(), 2f32 / Real::pi::<f32>().sqrt());
assert_approx_eq!(Real::sqrt2::<f32>(), 2f32.sqrt());
assert_approx_eq!(Real::frac_1_sqrt2::<f32>(), 1f32 / 2f32.sqrt());
assert_approx_eq!(Real::log2_e::<f32>(), Real::e::<f32>().log2());
assert_approx_eq!(Real::log10_e::<f32>(), Real::e::<f32>().log10());
assert_approx_eq!(Real::log_2::<f32>(), 2f32.log());
assert_approx_eq!(Real::log_10::<f32>(), 10f32.log());
}

#[test]
Expand Down Expand Up @@ -974,6 +981,15 @@ mod tests {
assert!(!NaN.is_negative());
}

#[test]
fn test_approx_eq() {
assert!(1.0f32.approx_eq(&1f32));
assert!(0.9999999f32.approx_eq(&1f32));
assert!(1.000001f32.approx_eq_eps(&1f32, &1.0e-5));
assert!(1.0000001f32.approx_eq_eps(&1f32, &1.0e-6));
assert!(!1.0000001f32.approx_eq_eps(&1f32, &1.0e-7));
}

#[test]
fn test_primitive() {
assert_eq!(Primitive::bits::<f32>(), sys::size_of::<f32>() * 8);
Expand Down
Loading

0 comments on commit b6dea9d

Please sign in to comment.