From c9073bec568efa90c85e381c12dbcd20fe86a8f0 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 5 Dec 2016 21:22:42 +0200 Subject: Refactored number traits --- src/num_traits.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'src/num_traits.rs') 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 + Sub + Mul + Div + Rem where Self: std::marker::Sized {} +impl ArithmeticOps for T where T: Add + Sub + Mul + Div + Rem {} + +pub trait SignedArithmeticOps: ArithmeticOps + Neg where Self: std::marker::Sized {} +impl SignedArithmeticOps for T where T: ArithmeticOps + Neg {} + + +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); -- cgit v1.2.3