Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ApproxEq::abs_diff_{eq|ne} functions and macros #16

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 96 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
//! # fn default_epsilon() -> T::Epsilon { T::default_epsilon() }
//! # fn default_max_relative() -> T::Epsilon { T::default_max_relative() }
//! # fn default_max_ulps() -> u32 { T::default_max_ulps() }
//! # fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { T::abs_diff_eq(&self.x, &other.x, epsilon) && T::abs_diff_eq(&self.i, &other.i, epsilon) }
//! # fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { T::relative_eq(&self.x, &other.x, epsilon, max_relative) && T::relative_eq(&self.i, &other.i, epsilon, max_relative) }
//! # fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { T::ulps_eq(&self.x, &other.x, epsilon, max_ulps) && T::ulps_eq(&self.i, &other.i, epsilon, max_ulps) }
//! # }
Expand Down Expand Up @@ -98,6 +99,11 @@
//! T::default_max_ulps()
//! }
//!
//! fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
//! T::abs_diff_eq(&self.x, &other.x, epsilon) &&
//! T::abs_diff_eq(&self.i, &other.i, epsilon)
//! }
//!
//! fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
//! T::relative_eq(&self.x, &other.x, epsilon, max_relative) &&
//! T::relative_eq(&self.i, &other.i, epsilon, max_relative)
Expand Down Expand Up @@ -152,13 +158,31 @@ pub trait ApproxEq: Sized {
/// This is used when no `max_relative` value is supplied to the `relative_eq` macro.
fn default_max_ulps() -> u32;

/// A test for equality that uses the absolute difference to compute the approximate
/// equality of two numbers.
fn abs_diff_eq(&self,
other: &Self,
epsilon: Self::Epsilon)
-> bool;

/// A test for equality that uses a relative comparison if the values are far apart.
fn relative_eq(&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon)
-> bool;

/// A test for equality that uses units in the last place (ULP) if the values are far apart.
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool;

/// The inverse of `ApproxEq::abs_diff_eq`.
fn abs_diff_ne(&self,
other: &Self,
epsilon: Self::Epsilon)
-> bool {
!Self::abs_diff_eq(self, other, epsilon)
}

/// The inverse of `ApproxEq::relative_eq`.
fn relative_ne(&self,
other: &Self,
Expand All @@ -168,9 +192,6 @@ pub trait ApproxEq: Sized {
!Self::relative_eq(self, other, epsilon, max_relative)
}

/// A test for equality that uses units in the last place (ULP) if the values are far apart.
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool;

/// The inverse of `ApproxEq::ulps_eq`.
fn ulps_ne(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
!Self::ulps_eq(self, other, epsilon, max_ulps)
Expand All @@ -191,6 +212,11 @@ macro_rules! impl_float_approx_eq {
#[inline]
fn default_max_ulps() -> u32 { 4 }

#[inline]
fn abs_diff_eq(&self, other: &$T, epsilon: $T) -> bool {
$T::abs(self - other) <= epsilon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this trait is intended to be possible to be implemented for unsigned integers, then you might run into underflow problems here. In rulinalg we handle this by a conditional on which element is bigger, see here. At the moment it's not a problem though

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, definitely.

}

#[inline]
fn relative_eq(&self, other: &$T, epsilon: $T, max_relative: $T) -> bool {
// Implementation based on: [Comparing Floating Point Numbers, 2012 Edition]
Expand Down Expand Up @@ -227,10 +253,8 @@ macro_rules! impl_float_approx_eq {
// Implementation based on: [Comparing Floating Point Numbers, 2012 Edition]
// (https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/)

let abs_diff = $T::abs(self - other);

// For when the numbers are really close together
if abs_diff <= epsilon {
if $T::abs_diff_eq(self, other, epsilon) {
return true;
}

Expand Down Expand Up @@ -271,6 +295,11 @@ impl<'a, T: ApproxEq> ApproxEq for &'a T {
T::default_max_ulps()
}

#[inline]
fn abs_diff_eq(&self, other: &&'a T, epsilon: T::Epsilon) -> bool {
T::abs_diff_eq(*self, *other, epsilon)
}

#[inline]
fn relative_eq(&self, other: &&'a T, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
T::relative_eq(*self, *other, epsilon, max_relative)
Expand Down Expand Up @@ -300,6 +329,11 @@ impl<'a, T: ApproxEq> ApproxEq for &'a mut T {
T::default_max_ulps()
}

#[inline]
fn abs_diff_eq(&self, other: &&'a mut T, epsilon: T::Epsilon) -> bool {
T::abs_diff_eq(*self, *other, epsilon)
}

#[inline]
fn relative_eq(&self,
other: &&'a mut T,
Expand All @@ -315,6 +349,62 @@ impl<'a, T: ApproxEq> ApproxEq for &'a mut T {
}
}

/// The requisite parameters for testing for approximate equality using a
/// absolute difference based comparison.
///
/// This is not normally used directly, rather via the
/// `assert_abs_diff_{eq|ne}!` and `abs_diff_{eq|ne}!` macros.
///
/// # Example
///
/// ```rust
/// use std::f64;
/// use approx::AbsDiff;
///
/// AbsDiff::default().eq(&1.0, &1.0);
/// AbsDiff::default().epsilon(f64::EPSILON).eq(&1.0, &1.0);
/// ```
pub struct AbsDiff<T: ApproxEq> {
/// The tolerance to use when testing values that are close together.
pub epsilon: T::Epsilon,
}

impl<T> Default for AbsDiff<T>
where T: ApproxEq
{
#[inline]
fn default() -> AbsDiff<T> {
AbsDiff {
epsilon: T::default_epsilon(),
}
}
}

impl<T> AbsDiff<T>
where T: ApproxEq
{
/// Replace the epsilon value with the one specified.
#[inline]
pub fn epsilon(self, epsilon: T::Epsilon) -> AbsDiff<T> {
AbsDiff {
epsilon: epsilon,
..self
}
}

/// Peform the equality comparison
#[inline]
pub fn eq(self, lhs: &T, rhs: &T) -> bool {
T::abs_diff_eq(lhs, rhs, self.epsilon)
}

/// Peform the inequality comparison
#[inline]
pub fn ne(self, lhs: &T, rhs: &T) -> bool {
T::abs_diff_ne(lhs, rhs, self.epsilon)
}
}

/// The requisite parameters for testing for approximate equality using a
/// relative based comparison.
///
Expand Down
106 changes: 106 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,111 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/// Predicate for testing the approximate equality of two values.
#[macro_export]
macro_rules! abs_diff_eq {
($lhs:expr, $rhs:expr, $($opt:ident = $opt_val:expr),+) => {{
$crate::AbsDiff::default()$(.$opt($opt_val))+.eq(&$lhs, &$rhs)
}};
($lhs:expr, $rhs:expr) => {{
$crate::AbsDiff::default().eq(&$lhs, &$rhs)
}};
}

/// Predicate for testing the approximate inequality of two values.
#[macro_export]
macro_rules! abs_diff_ne {
($lhs:expr, $rhs:expr, $($opt:ident = $opt_val:expr),+) => {{
$crate::AbsDiff::default()$(.$opt($opt_val))+.ne(&$lhs, &$rhs)
}};
($lhs:expr, $rhs:expr) => {{
$crate::AbsDiff::default().ne(&$lhs, &$rhs)
}};
}

#[macro_export]
macro_rules! assert_abs_diff_eq {
($given:expr, $expected:expr) => {{
let (given, expected) = (&($given), &($expected));

if !abs_diff_eq!(given, expected) {
panic!(
"assert_abs_diff_eq!({}, {})

left = {:?}
right = {:?}

",
stringify!($given), stringify!($expected),
given, expected,
);
}
}};
($given:expr, $expected:expr, $($opt:ident = $opt_val:expr),+) => {{
let (given, expected) = (&($given), &($expected));

if !abs_diff_eq!(given, expected, $($opt = $opt_val),+) {
panic!(
"assert_abs_diff_eq!({}, {}, {})

left = {:?}
right = {:?}

",
stringify!($given), stringify!($expected),
stringify!($($opt = $opt_val),+),
given, expected,
);
}
}};
($given:expr, $expected:expr,) => {
assert_abs_diff_eq!($given, $expected)
};
($given:expr, $expected:expr, $($opt:ident = $opt_val:expr,)+) => {
assert_abs_diff_eq!($given, $expected, $($opt = $opt_val),+)
};
}

#[macro_export]
macro_rules! assert_abs_diff_ne {
($given:expr, $expected:expr) => {{
let (given, expected) = (&($given), &($expected));

if !abs_diff_ne!(given, expected) {
panic!(
"assert_abs_diff_ne!({}, {})

left = {:?}
right = {:?}

",
stringify!($given), stringify!($expected),
given, expected,
);
}
}};
($given:expr, $expected:expr, $($opt:ident = $opt_val:expr),+) => {{
let (given, expected) = (&($given), &($expected));

if !abs_diff_ne!(given, expected, $($opt = $opt_val),+) {
panic!(
"assert_abs_diff_ne!({}, {}, {})

left = {:?}
right = {:?}

",
stringify!($given), stringify!($expected),
stringify!($($opt = $opt_val),+),
given, expected,
);
}
}};
($given:expr, $expected:expr,) => {
assert_abs_diff_ne!($given, $expected)
};
}

/// Predicate for testing the approximate equality of two values.
#[macro_export]
macro_rules! relative_eq {
Expand Down Expand Up @@ -224,3 +329,4 @@ macro_rules! assert_ulps_ne {
assert_ulps_ne!($given, $expected)
};
}

Loading