Skip to content

Commit

Permalink
Optionally support approx traits for proxy types.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Sep 27, 2019
1 parent 4fa3732 commit 1f5b00f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
20 changes: 13 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ keywords = ["finite", "float", "hash", "nan", "ord"]
travis-ci = { repository = "olson-sean-k/decorum" }

[features]
default = ["serialize-serde", "std"]
default = ["approx", "serialize-serde", "std"]
serialize-serde = ["serde", "serde_derive"]
std = ["num-traits/std"]
std = ["approx/std", "num-traits/std"]

[dependencies.approx]
version = "^0.3.0"
default-features = false
features = []
optional = true

[dependencies.num-traits]
version = "^0.2.4"
default-features = false
features = []

[dependencies.serde]
version = "1.0"
Expand All @@ -27,10 +38,5 @@ version = "1.0"
default-features = false
optional = true

[dependencies.num-traits]
version = "^0.2.4"
default-features = false
features = []

[dev-dependencies]
num = "^0.2.0"
58 changes: 57 additions & 1 deletion src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use core::cmp::Ordering;
use core::fmt::{self, Display, Formatter, LowerExp, UpperExp};
use core::hash::{Hash, Hasher};
Expand Down Expand Up @@ -212,7 +213,7 @@ where

// It is not possible to implement `From` for proxies in a generic way, because
// the `FloatConstraint` types `T` and `U` may be the same and conflict with
// the reflexive implementation in core. A similar problem prevents
// the reflexive implementation in `core`. A similar problem prevents
// implementing `From` over a type `T: Float`.

impl<T> From<NotNan<T>> for Ordered<T>
Expand Down Expand Up @@ -270,6 +271,23 @@ where
}
}

impl<T, P> AbsDiffEq for ConstrainedFloat<T, P>
where
T: AbsDiffEq<Epsilon = T> + Float + Primitive,
P: FloatConstraint<T> + ConstraintEq<T>,
{
type Epsilon = Self;

fn default_epsilon() -> Self::Epsilon {
Self::from_inner(T::default_epsilon())
}

fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.into_inner()
.abs_diff_eq(&other.into_inner(), epsilon.into_inner())
}
}

impl<T, P> Add for ConstrainedFloat<T, P>
where
T: Float + Primitive,
Expand Down Expand Up @@ -1273,6 +1291,29 @@ where
}
}

impl<T, P> RelativeEq for ConstrainedFloat<T, P>
where
T: Float + Primitive + RelativeEq<Epsilon = T>,
P: FloatConstraint<T> + ConstraintEq<T>,
{
fn default_max_relative() -> Self::Epsilon {
Self::from_inner(T::default_max_relative())
}

fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
self.into_inner().relative_eq(
&other.into_inner(),
epsilon.into_inner(),
max_relative.into_inner(),
)
}
}

impl<T, P> Rem for ConstrainedFloat<T, P>
where
T: Float + Primitive,
Expand Down Expand Up @@ -1468,6 +1509,21 @@ where
}
}

impl<T, P> UlpsEq for ConstrainedFloat<T, P>
where
T: Float + Primitive + UlpsEq<Epsilon = T>,
P: FloatConstraint<T> + ConstraintEq<T>,
{
fn default_max_ulps() -> u32 {
T::default_max_ulps()
}

fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
self.into_inner()
.ulps_eq(&other.into_inner(), epsilon.into_inner(), max_ulps)
}
}

impl<T, P> UpperExp for ConstrainedFloat<T, P>
where
T: Float + UpperExp + Primitive,
Expand Down

0 comments on commit 1f5b00f

Please sign in to comment.