diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2016-11-27 13:37:22 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2016-11-27 13:37:22 +0200 |
commit | 2da48d7549e2db3f0e8ba01523095d2f3764ce0a (patch) | |
tree | 04ea6f0b726bbfa04d4d14fc7007a891c51242a2 /src/num_traits.rs | |
parent | e4a79af68b2542bec5cd30237869cef0fc911f77 (diff) |
Added conjugating and multiplication
Diffstat (limited to 'src/num_traits.rs')
-rw-r--r-- | src/num_traits.rs | 124 |
1 files changed, 64 insertions, 60 deletions
diff --git a/src/num_traits.rs b/src/num_traits.rs index 3e18a4b..1e04cce 100644 --- a/src/num_traits.rs +++ b/src/num_traits.rs @@ -1,4 +1,4 @@ -pub trait Trig<T> { +pub trait Trig { fn sin(self) -> Self; fn cos(self) -> Self; fn tan(self) -> Self; @@ -8,73 +8,77 @@ pub trait Trig<T> { fn atan2(self, other: Self) -> Self; } -impl Trig<f32> for f32 { - fn sin(self) -> Self { - self.sin() - } - fn cos(self) -> Self { - self.cos() - } - fn tan(self) -> Self { - self.tan() - } - fn asin(self) -> Self { - self.asin() - } - fn acos(self) -> Self { - self.acos() - } - fn atan2(self, other: Self) -> Self { - self.atan2(other) - } -} -impl Trig<f64> for f64 { - fn sin(self) -> Self { - self.sin() - } - fn cos(self) -> Self { - self.cos() - } - fn tan(self) -> Self { - self.tan() - } - fn asin(self) -> Self { - self.asin() - } - fn acos(self) -> Self { - self.acos() - } - fn atan2(self, other: Self) -> Self { - self.atan2(other) +macro_rules! impl_float_trig { + ($t: ty) => { + impl Trig for $t { + fn sin(self) -> Self { + self.sin() + } + fn cos(self) -> Self { + self.cos() + } + fn tan(self) -> Self { + self.tan() + } + fn asin(self) -> Self { + self.asin() + } + fn acos(self) -> Self { + self.acos() + } + fn atan2(self, other: Self) -> Self { + self.atan2(other) + } + } } } +impl_float_trig!(f32); +impl_float_trig!(f64); + -pub trait Pow<T> { - fn powi(self, n: i32) -> Self; - fn powf(self, n: T) -> Self; +pub trait Pow { + fn pow(self, n: i32) -> Self; fn sqrt(self) -> Self; } -impl Pow<f32> for f32 { - fn powi(self, n: i32) -> Self { - self.powi(n) - } - fn powf(self, n: Self) -> Self { - self.powf(n) - } - fn sqrt(self) -> Self { - self.sqrt() +macro_rules! impl_float_pow { + ($t: ty) => { + impl Pow for $t { + fn pow(self, n: i32) -> Self { + self.powi(n) + } + fn sqrt(self) -> Self { + self.sqrt() + } + } } } -impl Pow<f64> for f64 { - fn powi(self, n: i32) -> Self { - self.powi(n) - } - fn powf(self, n: Self) -> Self { - self.powf(n) - } - fn sqrt(self) -> Self { - self.sqrt() + +macro_rules! impl_int_pow { + ($t: ty) => { + impl Pow for $t { + fn pow(self, n: i32) -> Self { + if n > 0 { + self.pow(n as u32) + } else { + (self as f64).powi(n) as Self + } + } + fn sqrt(self) -> Self { + (self as f64).sqrt() as Self + } + } } } + +impl_float_pow!(f32); +impl_float_pow!(f64); +impl_int_pow!(i8); +impl_int_pow!(i16); +impl_int_pow!(i32); +impl_int_pow!(i64); +impl_int_pow!(u8); +impl_int_pow!(u16); +impl_int_pow!(u32); +impl_int_pow!(u64); |