pub struct AbsDiff<A, B = A>
where
A: AbsDiffEq<B> + ?Sized,
B: ?Sized,
{
pub epsilon: A::Epsilon,
}
impl<A, B> Default for AbsDiff<A, B>
where
A: AbsDiffEq<B> + ?Sized,
B: ?Sized,
{
fn default() -> AbsDiff<A, B> {
AbsDiff {
epsilon: A::default_epsilon(),
}
}
}
impl<A, B> AbsDiff<A, B>
where
A: AbsDiffEq<B> + ?Sized,
B: ?Sized,
{
pub fn epsilon(self, epsilon: A::Epsilon) -> AbsDiff<A, B> {
AbsDiff { epsilon }
}
pub fn eq(self, lhs: &A, rhs: &B) -> bool {
A::abs_diff_eq(lhs, rhs, self.epsilon)
}
pub fn ne(self, lhs: &A, rhs: &B) -> bool {
A::abs_diff_ne(lhs, rhs, self.epsilon)
}
}
pub trait AbsDiffEq<Rhs = Self>: PartialEq<Rhs>
where
Rhs: ?Sized,
{
type Epsilon;
fn default_epsilon() -> Self::Epsilon;
fn abs_diff_eq(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool;
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool {
!Self::abs_diff_eq(self, other, epsilon)
}
}
macro_rules! impl_abs_diff_eq {
($T:ident) => {
impl AbsDiffEq for $T {
type Epsilon = $T;
fn default_epsilon() -> $T {
$T::EPSILON
}
fn abs_diff_eq(&self, other: &$T, epsilon: $T) -> bool {
(if self > other {
self - other
} else {
other - self
}) <= epsilon
}
}
};
}
impl_abs_diff_eq!(f32);
impl_abs_diff_eq!(f64);
#[macro_export]
macro_rules! abs_diff_eq {
($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
$crate::algebra::abstr::AbsDiff::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
};
($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
$crate::algebra::abstr::AbsDiff::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
};
}
#[macro_export]
macro_rules! abs_diff_ne {
($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
$crate::algebra::abstr::AbsDiff::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
};
($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
$crate::algebra::abstr::AbsDiff::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __assert_approx {
($eq:ident, $given:expr, $expected:expr) => {{
let (given, expected) = (&($given), &($expected));
if !$eq!(*given, *expected) {
panic!(
"assert_{}!({}, {})
left = {:?}
right = {:?}
",
stringify!($eq),
stringify!($given),
stringify!($expected),
given, expected,
);
}
}};
($eq:ident, $given:expr, $expected:expr, $($opt:ident = $val:expr),+) => {{
let (given, expected) = (&($given), &($expected));
if !$eq!(*given, *expected, $($opt = $val),+) {
panic!(
"assert_{}!({}, {}, {})
left = {:?}
right = {:?}
",
stringify!($eq),
stringify!($given),
stringify!($expected),
stringify!($($opt = $val),+),
given, expected,
);
}
}};
}
#[macro_export(local_inner_macros)]
macro_rules! assert_abs_diff_eq {
($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
__assert_approx!(abs_diff_eq, $given, $expected $(, $opt = $val)*)
};
($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
__assert_approx!(abs_diff_eq, $given, $expected $(, $opt = $val)*)
};
}
#[macro_export(local_inner_macros)]
macro_rules! assert_abs_diff_ne {
($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
__assert_approx!(abs_diff_ne, $given, $expected $(, $opt = $val)*)
};
($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
__assert_approx!(abs_diff_ne, $given, $expected $(, $opt = $val)*)
};
}