Skip to content

Commit

Permalink
Split Tolerance to absolute and relative
Browse files Browse the repository at this point in the history
  • Loading branch information
chalharu committed Oct 25, 2017
1 parent fcbef53 commit 5459605
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,38 @@ pub trait AbsError<Rhs: ?Sized = Self, Diff = Self> {
fn abs_error(&self, expected: &Rhs) -> ApproEqResult<Diff>;
}

#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
pub trait AbsTolerance<Diff = Self> {
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
fn abs_tolerance() -> Diff;
}

#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
pub trait RelTolerance<Diff = Self> {
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
fn rel_tolerance() -> Diff;
}

#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
pub trait Tolerance<Diff = Self> {
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
fn tolerance() -> Diff;
}

#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
impl<Diff: Tolerance<Diff>> AbsTolerance<Diff> for Diff {
fn abs_tolerance() -> Diff {
Diff::tolerance()
}
}

#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
impl<Diff: Tolerance<Diff>> RelTolerance<Diff> for Diff {
fn rel_tolerance() -> Diff {
Diff::tolerance()
}
}

/// Trait for approximately equality comparisons.
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
pub trait AbsApproEqWithTol<Rhs: ?Sized = Self, Diff = Self> {
Expand Down Expand Up @@ -179,10 +205,10 @@ pub trait AbsApproEq<Rhs: ?Sized = Self, Diff = Self> {
}

#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
impl<Rhs, Diff: PartialOrd + Tolerance<Diff>, T: AbsApproEqWithTol<Rhs, Diff>> AbsApproEq<Rhs, Diff>
impl<Rhs, Diff: PartialOrd + AbsTolerance<Diff>, T: AbsApproEqWithTol<Rhs, Diff>> AbsApproEq<Rhs, Diff>
for T {
fn abs_appro_eq(&self, other: &Rhs) -> bool {
self.abs_appro_eq_with_tol(other, &Diff::tolerance())
self.abs_appro_eq_with_tol(other, &Diff::abs_tolerance())
}
}

Expand Down Expand Up @@ -234,13 +260,14 @@ pub trait RelApproEq<Rhs: ?Sized = Self, Diff = Self> {
}

#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
impl<Rhs, Diff: PartialOrd + Tolerance<Diff>, T: RelApproEqWithTol<Rhs, Diff>> RelApproEq<Rhs, Diff>
impl<Rhs, Diff: PartialOrd + RelTolerance<Diff>, T: RelApproEqWithTol<Rhs, Diff>> RelApproEq<Rhs, Diff>
for T {
fn rel_appro_eq(&self, other: &Rhs) -> bool {
self.rel_appro_eq_with_tol(other, &Diff::tolerance())
self.rel_appro_eq_with_tol(other, &Diff::rel_tolerance())
}
}

/// tolerance is 1e-6 for f32
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
impl Tolerance for f32 {
fn tolerance() -> f32 {
Expand All @@ -266,6 +293,7 @@ impl RelError for f32 {
}
}

/// tolerance is 1e-6 for f64
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
impl Tolerance for f64 {
fn tolerance() -> f64 {
Expand Down Expand Up @@ -330,6 +358,7 @@ impl RelError<f32, f32> for f64 {
macro_rules! itype_impls {
($($T:ty)+) => {
$(
/// tolerance is zero for $T
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
impl Tolerance for $T {
fn tolerance() -> $T {
Expand Down Expand Up @@ -597,10 +626,19 @@ impl<A: ?Sized, D: PartialOrd, B: RelError<A, D> + ?Sized> RelError<RefCell<A>,
}
}

/// absolute tolerance is 1s for Duration
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
impl AbsTolerance for Duration {
fn abs_tolerance() -> Duration {
Duration::new(1, 0) // 1s
}
}

/// relative tolerance is 1ms(1/1000) for Duration
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.2.0"))]
impl Tolerance for Duration {
fn tolerance() -> Duration {
Duration::new(0, 1000000)
impl RelTolerance for Duration {
fn rel_tolerance() -> Duration {
Duration::new(0, 1000000) // 1ms (1/1000)
}
}

Expand Down

0 comments on commit 5459605

Please sign in to comment.