From 3d184aa3fb8cdf65701e47d2b3b259872bd4dba2 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Wed, 21 Jul 2021 22:23:44 +0800 Subject: [PATCH 1/3] Bump the minver of num-traits Signed-off-by: Chojan Shang --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 53bc610..d3ad2ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,4 +23,4 @@ ratio = [ "num-traits" ] std = [ ] [dependencies] -num-traits = { version = "0.2", default-features = false, optional = true } +num-traits = { version = "0.2.1", default-features = false, optional = true } From c8046415c7b62c2cfe117b187ced708e9011c237 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Wed, 21 Jul 2021 22:32:41 +0800 Subject: [PATCH 2/3] Make clippy happy Signed-off-by: Chojan Shang --- src/eq.rs | 8 ++++---- src/ulps_eq.rs | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/eq.rs b/src/eq.rs index fbc1a60..9798456 100644 --- a/src/eq.rs +++ b/src/eq.rs @@ -66,13 +66,13 @@ impl F32Margin { } pub fn epsilon(self, epsilon: f32) -> Self { F32Margin { - epsilon: epsilon, + epsilon, ..self } } pub fn ulps(self, ulps: i32) -> Self { F32Margin { - ulps: ulps, + ulps, ..self } } @@ -192,13 +192,13 @@ impl F64Margin { } pub fn epsilon(self, epsilon: f64) -> Self { F64Margin { - epsilon: epsilon, + epsilon, ..self } } pub fn ulps(self, ulps: i64) -> Self { F64Margin { - ulps: ulps, + ulps, ..self } } diff --git a/src/ulps_eq.rs b/src/ulps_eq.rs index 56a529a..ea6f116 100644 --- a/src/ulps_eq.rs +++ b/src/ulps_eq.rs @@ -1,6 +1,9 @@ // Copyright 2014-2020 Optimal Computing (NZ) Ltd. // Licensed under the MIT license. See LICENSE for details. +#[cfg(feature = "num-traits")] +#[allow(unused_imports)] +use num_traits::float::FloatCore; use super::Ulps; /// ApproxEqUlps is a trait for approximate equality comparisons. @@ -34,7 +37,7 @@ impl ApproxEqUlps for f32 { fn approx_eq_ulps(&self, other: &f32, ulps: i32) -> bool { // -0 and +0 are drastically far in ulps terms, so // we need a special case for that. - if *self==*other { return true; } + if (*self - *other).abs() < core::f32::EPSILON { return true; } // Handle differing signs as a special case, even if // they are very close, most people consider them @@ -77,7 +80,7 @@ impl ApproxEqUlps for f64 { fn approx_eq_ulps(&self, other: &f64, ulps: i64) -> bool { // -0 and +0 are drastically far in ulps terms, so // we need a special case for that. - if *self==*other { return true; } + if (*self - *other).abs() < core::f64::EPSILON / 2.0 { return true; } // Handle differing signs as a special case, even if // they are very close, most people consider them From d76d1b10e1b2cc6458aa8d4fdaf61f29d7bd5e9c Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Thu, 22 Jul 2021 11:08:21 +1200 Subject: [PATCH 3/3] Revert to direct equality testing (it is for the zero/sign case only) --- src/ulps_eq.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ulps_eq.rs b/src/ulps_eq.rs index ea6f116..835a614 100644 --- a/src/ulps_eq.rs +++ b/src/ulps_eq.rs @@ -37,7 +37,7 @@ impl ApproxEqUlps for f32 { fn approx_eq_ulps(&self, other: &f32, ulps: i32) -> bool { // -0 and +0 are drastically far in ulps terms, so // we need a special case for that. - if (*self - *other).abs() < core::f32::EPSILON { return true; } + if *self==*other { return true; } // Handle differing signs as a special case, even if // they are very close, most people consider them @@ -80,7 +80,7 @@ impl ApproxEqUlps for f64 { fn approx_eq_ulps(&self, other: &f64, ulps: i64) -> bool { // -0 and +0 are drastically far in ulps terms, so // we need a special case for that. - if (*self - *other).abs() < core::f64::EPSILON / 2.0 { return true; } + if *self==*other { return true; } // Handle differing signs as a special case, even if // they are very close, most people consider them