Skip to content

Commit 37a610a

Browse files
committed
auto merge of #4831 : bjz/rust/incoming, r=pcwalton
This is useful for comparing more complex types that include floats.
2 parents 3764cfb + 7651100 commit 37a610a

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/libstd/cmp.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ use core::f32;
1414
use core::f64;
1515
use core::float;
1616

17-
const fuzzy_epsilon: float = 1.0e-6;
17+
pub const FUZZY_EPSILON: float = 1.0e-6;
1818

19-
pub trait FuzzyEq {
19+
pub trait FuzzyEq<Eps> {
2020
pure fn fuzzy_eq(&self, other: &Self) -> bool;
21-
pure fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Self) -> bool;
21+
pure fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool;
2222
}
2323

24-
impl float: FuzzyEq {
24+
impl float: FuzzyEq<float> {
2525
pure fn fuzzy_eq(&self, other: &float) -> bool {
26-
self.fuzzy_eq_eps(other, &fuzzy_epsilon)
26+
self.fuzzy_eq_eps(other, &FUZZY_EPSILON)
2727
}
2828

2929
pure fn fuzzy_eq_eps(&self, other: &float, epsilon: &float) -> bool {
3030
float::abs(*self - *other) < *epsilon
3131
}
3232
}
3333

34-
impl f32: FuzzyEq {
34+
impl f32: FuzzyEq<f32> {
3535
pure fn fuzzy_eq(&self, other: &f32) -> bool {
36-
self.fuzzy_eq_eps(other, &(fuzzy_epsilon as f32))
36+
self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32))
3737
}
3838

3939
pure fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool {
4040
f32::abs(*self - *other) < *epsilon
4141
}
4242
}
4343

44-
impl f64: FuzzyEq {
44+
impl f64: FuzzyEq<f64> {
4545
pure fn fuzzy_eq(&self, other: &f64) -> bool {
46-
self.fuzzy_eq_eps(other, &(fuzzy_epsilon as f64))
46+
self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64))
4747
}
4848

4949
pure fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool {
@@ -63,3 +63,40 @@ fn test_fuzzy_eq_eps() {
6363
assert (&1.2f).fuzzy_eq_eps(&0.9, &0.5);
6464
assert !(&1.5f).fuzzy_eq_eps(&0.9, &0.5);
6565
}
66+
67+
#[test]
68+
mod test_complex{
69+
use cmp::*;
70+
71+
struct Complex { r: float, i: float }
72+
73+
impl Complex: FuzzyEq<float> {
74+
pure fn fuzzy_eq(&self, other: &Complex) -> bool {
75+
self.fuzzy_eq_eps(other, &FUZZY_EPSILON)
76+
}
77+
78+
pure fn fuzzy_eq_eps(&self, other: &Complex,
79+
epsilon: &float) -> bool {
80+
self.r.fuzzy_eq_eps(&other.r, epsilon) &&
81+
self.i.fuzzy_eq_eps(&other.i, epsilon)
82+
}
83+
}
84+
85+
#[test]
86+
fn test_fuzzy_equals() {
87+
let a = Complex {r: 0.9, i: 0.9};
88+
let b = Complex {r: 0.9, i: 0.9};
89+
90+
assert (a.fuzzy_eq(&b));
91+
}
92+
93+
#[test]
94+
fn test_fuzzy_eq_eps() {
95+
let other = Complex {r: 0.9, i: 0.9};
96+
97+
assert (&Complex {r: 0.9, i: 1.2}).fuzzy_eq_eps(&other, &0.5);
98+
assert (&Complex {r: 1.2, i: 0.9}).fuzzy_eq_eps(&other, &0.5);
99+
assert !(&Complex {r: 0.9, i: 1.5}).fuzzy_eq_eps(&other, &0.5);
100+
assert !(&Complex {r: 1.5, i: 0.9}).fuzzy_eq_eps(&other, &0.5);
101+
}
102+
}

src/test/run-pass/trait-inheritance-num.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::cmp::FuzzyEq;
1818

1919
pub trait NumExt: Num Eq Ord {}
2020

21-
pub trait FloatExt: NumExt FuzzyEq {}
21+
pub trait FloatExt: NumExt FuzzyEq<Self> {}
2222

2323
fn greater_than_one<T:NumExt>(n: &T) -> bool { *n > from_int(1) }
2424
fn greater_than_one_float<T:FloatExt>(n: &T) -> bool { *n > from_int(1) }

src/test/run-pass/trait-inheritance-num2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub impl i64: IntegerExt {}
9494
pub impl int: IntegerExt {}
9595

9696

97-
pub trait FloatExt: NumExt FuzzyEq {}
97+
pub trait FloatExt: NumExt FuzzyEq<Self> {}
9898

9999
pub impl f32: FloatExt {}
100100
pub impl f64: FloatExt {}

0 commit comments

Comments
 (0)