diff --git a/src/lib.rs b/src/lib.rs index bf98f2b..14bb3d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,14 +34,13 @@ pub use float::FloatConst; // pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`. pub use identities::{Zero, One, zero, one}; pub use ops::inv::Inv; -pub use ops::pow::Pow; pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, CheckedShl, CheckedShr}; pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub}; pub use ops::saturating::Saturating; pub use sign::{Signed, Unsigned, abs, abs_sub, signum}; pub use cast::{AsPrimitive, FromPrimitive, ToPrimitive, NumCast, cast}; pub use int::PrimInt; -pub use pow::{pow, checked_pow}; +pub use pow::{Pow, pow, checked_pow}; pub mod identities; pub mod sign; diff --git a/src/ops/mod.rs b/src/ops/mod.rs index 6f7f022..7b9a7e3 100644 --- a/src/ops/mod.rs +++ b/src/ops/mod.rs @@ -2,4 +2,3 @@ pub mod saturating; pub mod checked; pub mod wrapping; pub mod inv; -pub mod pow; diff --git a/src/ops/pow.rs b/src/ops/pow.rs deleted file mode 100644 index 9f7dd1e..0000000 --- a/src/ops/pow.rs +++ /dev/null @@ -1,71 +0,0 @@ -/// Binary operator for raising a value to a power. -pub trait Pow { - /// The result after applying the operator. - type Output; - - /// Returns `self` to the power `rhs`. - /// - /// # Examples - /// - /// ``` - /// use num_traits::Pow; - /// assert_eq!(10.pow(2), 100); - /// ``` - fn pow(self, rhs: RHS) -> Self::Output; -} - -macro_rules! pow_impl { - ($t:ty, $rhs:ty, $method:ident) => { - impl Pow<$rhs> for $t { - type Output = $t; - - #[inline] - fn pow(self, rhs: $rhs) -> $t { - <$t>::$method(self, rhs) - } - } - - impl<'a> Pow<&'a $rhs> for $t { - type Output = $t; - #[inline] - fn pow(self, rhs: &'a $rhs) -> $t { - <$t>::$method(self, *rhs) - } - } - - impl<'a> Pow<$rhs> for &'a $t { - type Output = $t; - #[inline] - fn pow(self, rhs: $rhs) -> $t { - <$t>::$method(*self, rhs) - } - } - - impl<'a, 'b> Pow<&'a $rhs> for &'b $t { - type Output = $t; - #[inline] - fn pow(self, rhs: &'a $rhs) -> $t { - <$t>::$method(*self, *rhs) - } - } - } -} - -pow_impl!(u8, u32, pow); -pow_impl!(i8, u32, pow); -pow_impl!(u16, u32, pow); -pow_impl!(i16, u32, pow); -pow_impl!(u32, u32, pow); -pow_impl!(i32, u32, pow); -pow_impl!(u64, u32, pow); -pow_impl!(i64, u32, pow); -pow_impl!(usize, u32, pow); -pow_impl!(isize, u32, pow); - -#[cfg(feature = "std")] -mod float_impls { - pow_impl!(f32, i32, powi); - pow_impl!(f64, i32, powi); - pow_impl!(f32, f32, powf); - pow_impl!(f64, f64, powf); -} diff --git a/src/pow.rs b/src/pow.rs index 212a1de..352d30c 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -1,6 +1,81 @@ use core::ops::Mul; use {One, CheckedMul}; +/// Binary operator for raising a value to a power. +pub trait Pow { + /// The result after applying the operator. + type Output; + + /// Returns `self` to the power `rhs`. + /// + /// # Examples + /// + /// ``` + /// use num_traits::Pow; + /// assert_eq!(10.pow(2), 100); + /// ``` + fn pow(self, rhs: RHS) -> Self::Output; +} + +macro_rules! pow_impl { + ($t:ty, $rhs:ty, $method:ident) => { + impl Pow<$rhs> for $t { + type Output = $t; + + #[inline] + fn pow(self, rhs: $rhs) -> $t { + <$t>::$method(self, rhs) + } + } + + impl<'a> Pow<&'a $rhs> for $t { + type Output = $t; + #[inline] + fn pow(self, rhs: &'a $rhs) -> $t { + <$t>::$method(self, *rhs) + } + } + + impl<'a> Pow<$rhs> for &'a $t { + type Output = $t; + #[inline] + fn pow(self, rhs: $rhs) -> $t { + <$t>::$method(*self, rhs) + } + } + + impl<'a, 'b> Pow<&'a $rhs> for &'b $t { + type Output = $t; + #[inline] + fn pow(self, rhs: &'a $rhs) -> $t { + <$t>::$method(*self, *rhs) + } + } + } +} + +pow_impl!(u8, u32, pow); +pow_impl!(i8, u32, pow); +pow_impl!(u16, u32, pow); +pow_impl!(i16, u32, pow); +pow_impl!(u32, u32, pow); +pow_impl!(i32, u32, pow); +pow_impl!(u64, u32, pow); +pow_impl!(i64, u32, pow); +pow_impl!(usize, u32, pow); +pow_impl!(isize, u32, pow); + +#[cfg(feature = "std")] +mod float_impls { + use super::Pow; + + pow_impl!(f32, i32, powi); + pow_impl!(f64, i32, powi); + pow_impl!(f32, f32, powf); + pow_impl!(f64, f64, powf); +} + + /// Raises a value to the power of exp, using exponentiation by squaring. /// /// # Example