From b081f594954385813fc3b3b0d26cfcc191f1ceeb Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 7 Feb 2013 10:25:41 +1100 Subject: [PATCH 1/3] Convert fuzzy_epsilon constant to upper case and make public --- src/libstd/cmp.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index 0bd7538d9472..c3c6fc029df4 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -15,7 +15,7 @@ use core::f32; use core::f64; use core::float; -const fuzzy_epsilon: float = 1.0e-6; +pub const FUZZY_EPSILON: float = 1.0e-6; pub trait FuzzyEq { pure fn fuzzy_eq(&self, other: &Self) -> bool; @@ -24,7 +24,7 @@ pub trait FuzzyEq { impl float: FuzzyEq { pure fn fuzzy_eq(&self, other: &float) -> bool { - self.fuzzy_eq_eps(other, &fuzzy_epsilon) + self.fuzzy_eq_eps(other, &FUZZY_EPSILON) } pure fn fuzzy_eq_eps(&self, other: &float, epsilon: &float) -> bool { @@ -34,7 +34,7 @@ impl float: FuzzyEq { impl f32: FuzzyEq { pure fn fuzzy_eq(&self, other: &f32) -> bool { - self.fuzzy_eq_eps(other, &(fuzzy_epsilon as f32)) + self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32)) } pure fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool { @@ -44,7 +44,7 @@ impl f32: FuzzyEq { impl f64: FuzzyEq { pure fn fuzzy_eq(&self, other: &f64) -> bool { - self.fuzzy_eq_eps(other, &(fuzzy_epsilon as f64)) + self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64)) } pure fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool { From e4c7d8ec8764d1daf8b247c359d564daea1c113c Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 7 Feb 2013 22:54:52 +1100 Subject: [PATCH 2/3] Add type parameter for epsilon value --- src/libstd/cmp.rs | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index c3c6fc029df4..6e783a4be7f0 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -17,12 +17,12 @@ use core::float; pub const FUZZY_EPSILON: float = 1.0e-6; -pub trait FuzzyEq { +pub trait FuzzyEq { pure fn fuzzy_eq(&self, other: &Self) -> bool; - pure fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Self) -> bool; + pure fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool; } -impl float: FuzzyEq { +impl float: FuzzyEq { pure fn fuzzy_eq(&self, other: &float) -> bool { self.fuzzy_eq_eps(other, &FUZZY_EPSILON) } @@ -32,7 +32,7 @@ impl float: FuzzyEq { } } -impl f32: FuzzyEq { +impl f32: FuzzyEq { pure fn fuzzy_eq(&self, other: &f32) -> bool { self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32)) } @@ -42,7 +42,7 @@ impl f32: FuzzyEq { } } -impl f64: FuzzyEq { +impl f64: FuzzyEq { pure fn fuzzy_eq(&self, other: &f64) -> bool { self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64)) } @@ -64,3 +64,40 @@ fn test_fuzzy_eq_eps() { assert (&1.2f).fuzzy_eq_eps(&0.9, &0.5); assert !(&1.5f).fuzzy_eq_eps(&0.9, &0.5); } + +#[test] +mod test_complex{ + use cmp::*; + + struct Complex { r: float, i: float } + + impl Complex: FuzzyEq { + pure fn fuzzy_eq(&self, other: &Complex) -> bool { + self.fuzzy_eq_eps(other, &FUZZY_EPSILON) + } + + pure fn fuzzy_eq_eps(&self, other: &Complex, + epsilon: &float) -> bool { + self.r.fuzzy_eq_eps(&other.r, epsilon) && + self.i.fuzzy_eq_eps(&other.i, epsilon) + } + } + + #[test] + fn test_fuzzy_equals() { + let a = Complex {r: 0.9, i: 0.9}; + let b = Complex {r: 0.9, i: 0.9}; + + assert (a.fuzzy_eq(&b)); + } + + #[test] + fn test_fuzzy_eq_eps() { + let other = Complex {r: 0.9, i: 0.9}; + + assert (&Complex {r: 0.9, i: 1.2}).fuzzy_eq_eps(&other, &0.5); + assert (&Complex {r: 1.2, i: 0.9}).fuzzy_eq_eps(&other, &0.5); + assert !(&Complex {r: 0.9, i: 1.5}).fuzzy_eq_eps(&other, &0.5); + assert !(&Complex {r: 1.5, i: 0.9}).fuzzy_eq_eps(&other, &0.5); + } +} From 7651100ec104c97eea486e2576bd1f058f4ff592 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 8 Feb 2013 02:41:23 +1100 Subject: [PATCH 3/3] Fix broken tests --- src/test/run-pass/trait-inheritance-num.rs | 2 +- src/test/run-pass/trait-inheritance-num2.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/run-pass/trait-inheritance-num.rs b/src/test/run-pass/trait-inheritance-num.rs index 90e7db7dbbb3..ca720c27d77a 100644 --- a/src/test/run-pass/trait-inheritance-num.rs +++ b/src/test/run-pass/trait-inheritance-num.rs @@ -18,7 +18,7 @@ use std::cmp::FuzzyEq; pub trait NumExt: Num Eq Ord {} -pub trait FloatExt: NumExt FuzzyEq {} +pub trait FloatExt: NumExt FuzzyEq {} fn greater_than_one(n: &T) -> bool { *n > from_int(1) } fn greater_than_one_float(n: &T) -> bool { *n > from_int(1) } diff --git a/src/test/run-pass/trait-inheritance-num2.rs b/src/test/run-pass/trait-inheritance-num2.rs index 455b3946ec05..ecedaac8daaa 100644 --- a/src/test/run-pass/trait-inheritance-num2.rs +++ b/src/test/run-pass/trait-inheritance-num2.rs @@ -94,7 +94,7 @@ pub impl i64: IntegerExt {} pub impl int: IntegerExt {} -pub trait FloatExt: NumExt FuzzyEq {} +pub trait FloatExt: NumExt FuzzyEq {} pub impl f32: FloatExt {} pub impl f64: FloatExt {}