diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2016-12-05 21:22:42 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2016-12-05 21:22:42 +0200 |
commit | c9073bec568efa90c85e381c12dbcd20fe86a8f0 (patch) | |
tree | 914e35b477fdc607ae0844b673bf8dde9552c22c /src/num_traits.rs | |
parent | 308edb5b5cd5ee6d2e2962f627612e3973f5b586 (diff) |
Refactored number traits
Diffstat (limited to 'src/num_traits.rs')
-rw-r--r-- | src/num_traits.rs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/num_traits.rs b/src/num_traits.rs index a754787..030e5c4 100644 --- a/src/num_traits.rs +++ b/src/num_traits.rs @@ -86,23 +86,36 @@ impl_int_pow!(u32); impl_int_pow!(u64); -pub trait Float { +use std::ops::{Add, Sub, Mul, Div, Rem, Neg}; + +pub trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> where Self: std::marker::Sized {} +impl<T> ArithmeticOps for T where T: Add<Output=T> + Sub<Output=T> + Mul<Output=T> + Div<Output=T> + Rem<Output=T> {} + +pub trait SignedArithmeticOps: ArithmeticOps + Neg<Output=Self> where Self: std::marker::Sized {} +impl<T> SignedArithmeticOps for T where T: ArithmeticOps + Neg<Output=T> {} + + +pub trait FractionOps { fn recip(self) -> Self; fn pi() -> Self; + fn two_pi() -> Self; } -macro_rules! impl_float { +macro_rules! impl_fraction_float { ($t: ty, $pi: expr) => { - impl Float for $t { + impl FractionOps for $t { fn recip(self) -> Self { self.recip() } fn pi() -> Self { $pi } + fn two_pi() -> Self { + 2.0 * $pi + } } } } -impl_float!(f32, std::f32::consts::PI); -impl_float!(f64, std::f64::consts::PI); +impl_fraction_float!(f32, std::f32::consts::PI); +impl_fraction_float!(f64, std::f64::consts::PI); |