From 52975202afdd9858215f28e567940cb768c7c36c Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 28 Jan 2013 19:28:23 -0500 Subject: [PATCH 1/4] fix std::cmp docstring --- src/libstd/cmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index 7c126c7338b71..27010ab5cf89d 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -9,7 +9,7 @@ // except according to those terms. #[forbid(deprecated_mode)]; -/// Additional general-purpose comparison functionality. +//! Additional general-purpose comparison functionality. use core::f32; use core::f64; From abb79cb52de957a9c634ee2f7192627d92dd697f Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 28 Jan 2013 19:28:40 -0500 Subject: [PATCH 2/4] update FuzzyEq to explicit self --- src/libstd/cmp.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index 27010ab5cf89d..c06e22dfbb135 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -18,31 +18,30 @@ use core::float; const fuzzy_epsilon: float = 1.0e-6; pub trait FuzzyEq { - pure fn fuzzy_eq(other: &self) -> bool; + pure fn fuzzy_eq(&self, other: &self) -> bool; } impl float: FuzzyEq { - pure fn fuzzy_eq(other: &float) -> bool { - return float::abs(self - *other) < fuzzy_epsilon; + pure fn fuzzy_eq(&self, other: &float) -> bool { + float::abs(*self - *other) < fuzzy_epsilon } } impl f32: FuzzyEq { - pure fn fuzzy_eq(other: &f32) -> bool { - return f32::abs(self - *other) < (fuzzy_epsilon as f32); + pure fn fuzzy_eq(&self, other: &f32) -> bool { + f32::abs(*self - *other) < (fuzzy_epsilon as f32) } } impl f64: FuzzyEq { - pure fn fuzzy_eq(other: &f64) -> bool { - return f64::abs(self - *other) < (fuzzy_epsilon as f64); + pure fn fuzzy_eq(&self, other: &f64) -> bool { + f64::abs(*self - *other) < (fuzzy_epsilon as f64) } } #[test] fn test_fuzzy_equals() { - assert ((&1.0).fuzzy_eq(&1.0)); - assert ((&1.0f32).fuzzy_eq(&1.0f32)); - assert ((&1.0f64).fuzzy_eq(&1.0f64)); + assert (&1.0).fuzzy_eq(&1.0); + assert (&1.0f32).fuzzy_eq(&1.0f32); + assert (&1.0f64).fuzzy_eq(&1.0f64); } - From fce6446e7e2683b5cfcc2a36211cec5ab2c355fa Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 28 Jan 2013 21:12:25 -0500 Subject: [PATCH 3/4] add a FuzzyEq method that accepts an epsilon value --- src/libstd/cmp.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index c06e22dfbb135..d6fc23c6fdd9d 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -19,24 +19,37 @@ const fuzzy_epsilon: float = 1.0e-6; pub trait FuzzyEq { pure fn fuzzy_eq(&self, other: &self) -> bool; + pure fn fuzzy_eq_eps(&self, other: &self, epsilon: &self) -> bool; } impl float: FuzzyEq { pure fn fuzzy_eq(&self, other: &float) -> bool { float::abs(*self - *other) < fuzzy_epsilon } + + pure fn fuzzy_eq_eps(&self, other: &float, epsilon: &float) -> bool { + float::abs(*self - *other) < *epsilon + } } impl f32: FuzzyEq { pure fn fuzzy_eq(&self, other: &f32) -> bool { f32::abs(*self - *other) < (fuzzy_epsilon as f32) } + + pure fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool { + f32::abs(*self - *other) < *epsilon + } } impl f64: FuzzyEq { pure fn fuzzy_eq(&self, other: &f64) -> bool { f64::abs(*self - *other) < (fuzzy_epsilon as f64) } + + pure fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool { + f64::abs(*self - *other) < *epsilon + } } #[test] @@ -45,3 +58,9 @@ fn test_fuzzy_equals() { assert (&1.0f32).fuzzy_eq(&1.0f32); assert (&1.0f64).fuzzy_eq(&1.0f64); } + +#[test] +fn test_fuzzy_eq_eps() { + assert (&1.2).fuzzy_eq_eps(&0.9, &0.5); + assert !(&1.5).fuzzy_eq_eps(&0.9, &0.5); +} From e0728d41f2fe9c87e086dc5bcc0260dcffcb7517 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 29 Jan 2013 15:35:26 -0500 Subject: [PATCH 4/4] implement fuzzy_eq with fuzzy_eq_eps --- 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 d6fc23c6fdd9d..ef893b067be89 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -24,7 +24,7 @@ pub trait FuzzyEq { impl float: FuzzyEq { pure fn fuzzy_eq(&self, other: &float) -> bool { - float::abs(*self - *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 { - f32::abs(*self - *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 { - f64::abs(*self - *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 {