2018-02-23 22:43:27 +00:00
|
|
|
use core::num::Wrapping;
|
2018-07-13 00:09:22 +00:00
|
|
|
use core::ops::Mul;
|
|
|
|
use {CheckedMul, One};
|
2016-05-09 15:02:21 +00:00
|
|
|
|
2018-02-19 20:17:41 +00:00
|
|
|
/// Binary operator for raising a value to a power.
|
|
|
|
pub trait Pow<RHS> {
|
|
|
|
/// The result after applying the operator.
|
|
|
|
type Output;
|
|
|
|
|
|
|
|
/// Returns `self` to the power `rhs`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use num_traits::Pow;
|
2018-02-23 22:20:08 +00:00
|
|
|
/// assert_eq!(Pow::pow(10u32, 2u32), 100);
|
2018-02-19 20:17:41 +00:00
|
|
|
/// ```
|
|
|
|
fn pow(self, rhs: RHS) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! pow_impl {
|
2018-02-23 22:43:27 +00:00
|
|
|
($t:ty) => {
|
|
|
|
pow_impl!($t, u8);
|
|
|
|
pow_impl!($t, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
|
|
|
|
// FIXME: these should be possible
|
|
|
|
// pow_impl!($t, u16);
|
|
|
|
// pow_impl!($t, u32);
|
|
|
|
// pow_impl!($t, u64);
|
2018-02-23 22:43:27 +00:00
|
|
|
};
|
|
|
|
($t:ty, $rhs:ty) => {
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!($t, $rhs, usize, pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
};
|
2018-02-27 19:06:46 +00:00
|
|
|
($t:ty, $rhs:ty, $desired_rhs:ty, $method:expr) => {
|
2018-02-19 20:17:41 +00:00
|
|
|
impl Pow<$rhs> for $t {
|
|
|
|
type Output = $t;
|
|
|
|
#[inline]
|
|
|
|
fn pow(self, rhs: $rhs) -> $t {
|
2018-02-27 19:06:46 +00:00
|
|
|
($method)(self, <$desired_rhs>::from(rhs))
|
2018-02-19 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Pow<&'a $rhs> for $t {
|
|
|
|
type Output = $t;
|
|
|
|
#[inline]
|
|
|
|
fn pow(self, rhs: &'a $rhs) -> $t {
|
2018-02-27 19:06:46 +00:00
|
|
|
($method)(self, <$desired_rhs>::from(*rhs))
|
2018-02-19 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Pow<$rhs> for &'a $t {
|
|
|
|
type Output = $t;
|
|
|
|
#[inline]
|
|
|
|
fn pow(self, rhs: $rhs) -> $t {
|
2018-02-27 19:06:46 +00:00
|
|
|
($method)(*self, <$desired_rhs>::from(rhs))
|
2018-02-19 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Pow<&'a $rhs> for &'b $t {
|
|
|
|
type Output = $t;
|
|
|
|
#[inline]
|
|
|
|
fn pow(self, rhs: &'a $rhs) -> $t {
|
2018-02-27 19:06:46 +00:00
|
|
|
($method)(*self, <$desired_rhs>::from(*rhs))
|
2018-02-19 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 22:43:27 +00:00
|
|
|
};
|
2018-02-19 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(u8, u8, u32, u8::pow);
|
|
|
|
pow_impl!(u8, u16, u32, u8::pow);
|
|
|
|
pow_impl!(u8, u32, u32, u8::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(u8, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(i8, u8, u32, i8::pow);
|
|
|
|
pow_impl!(i8, u16, u32, i8::pow);
|
|
|
|
pow_impl!(i8, u32, u32, i8::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(i8, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(u16, u8, u32, u16::pow);
|
|
|
|
pow_impl!(u16, u16, u32, u16::pow);
|
|
|
|
pow_impl!(u16, u32, u32, u16::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(u16, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(i16, u8, u32, i16::pow);
|
|
|
|
pow_impl!(i16, u16, u32, i16::pow);
|
|
|
|
pow_impl!(i16, u32, u32, i16::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(i16, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(u32, u8, u32, u32::pow);
|
|
|
|
pow_impl!(u32, u16, u32, u32::pow);
|
|
|
|
pow_impl!(u32, u32, u32, u32::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(u32, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(i32, u8, u32, i32::pow);
|
|
|
|
pow_impl!(i32, u16, u32, i32::pow);
|
|
|
|
pow_impl!(i32, u32, u32, i32::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(i32, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(u64, u8, u32, u64::pow);
|
|
|
|
pow_impl!(u64, u16, u32, u64::pow);
|
|
|
|
pow_impl!(u64, u32, u32, u64::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(u64, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(i64, u8, u32, i64::pow);
|
|
|
|
pow_impl!(i64, u16, u32, i64::pow);
|
|
|
|
pow_impl!(i64, u32, u32, i64::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(i64, usize);
|
2018-04-09 09:11:15 +00:00
|
|
|
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(u128, u8, u32, u128::pow);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(u128, u16, u32, u128::pow);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(u128, u32, u32, u128::pow);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(u128, usize);
|
|
|
|
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(i128, u8, u32, i128::pow);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(i128, u16, u32, i128::pow);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(i128, u32, u32, i128::pow);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(i128, usize);
|
|
|
|
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(usize, u8, u32, usize::pow);
|
|
|
|
pow_impl!(usize, u16, u32, usize::pow);
|
|
|
|
pow_impl!(usize, u32, u32, usize::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(usize, usize);
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(isize, u8, u32, isize::pow);
|
|
|
|
pow_impl!(isize, u16, u32, isize::pow);
|
|
|
|
pow_impl!(isize, u32, u32, isize::pow);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(isize, usize);
|
|
|
|
pow_impl!(Wrapping<u8>);
|
|
|
|
pow_impl!(Wrapping<i8>);
|
|
|
|
pow_impl!(Wrapping<u16>);
|
|
|
|
pow_impl!(Wrapping<i16>);
|
|
|
|
pow_impl!(Wrapping<u32>);
|
|
|
|
pow_impl!(Wrapping<i32>);
|
|
|
|
pow_impl!(Wrapping<u64>);
|
|
|
|
pow_impl!(Wrapping<i64>);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(Wrapping<u128>);
|
2018-05-11 22:50:48 +00:00
|
|
|
#[cfg(has_i128)]
|
2018-04-09 09:11:15 +00:00
|
|
|
pow_impl!(Wrapping<i128>);
|
2018-02-23 22:43:27 +00:00
|
|
|
pow_impl!(Wrapping<usize>);
|
|
|
|
pow_impl!(Wrapping<isize>);
|
2018-02-19 20:17:41 +00:00
|
|
|
|
2018-02-27 19:06:46 +00:00
|
|
|
// FIXME: these should be possible
|
|
|
|
// pow_impl!(u8, u64);
|
|
|
|
// pow_impl!(i16, u64);
|
|
|
|
// pow_impl!(i8, u64);
|
|
|
|
// pow_impl!(u16, u64);
|
|
|
|
// pow_impl!(u32, u64);
|
|
|
|
// pow_impl!(i32, u64);
|
|
|
|
// pow_impl!(u64, u64);
|
|
|
|
// pow_impl!(i64, u64);
|
|
|
|
// pow_impl!(usize, u64);
|
|
|
|
// pow_impl!(isize, u64);
|
|
|
|
|
2018-02-19 20:17:41 +00:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
mod float_impls {
|
|
|
|
use super::Pow;
|
|
|
|
|
2018-02-27 19:06:46 +00:00
|
|
|
pow_impl!(f32, i8, i32, f32::powi);
|
|
|
|
pow_impl!(f32, u8, i32, f32::powi);
|
|
|
|
pow_impl!(f32, i16, i32, f32::powi);
|
|
|
|
pow_impl!(f32, u16, i32, f32::powi);
|
|
|
|
pow_impl!(f32, i32, i32, f32::powi);
|
|
|
|
pow_impl!(f64, i8, i32, f64::powi);
|
|
|
|
pow_impl!(f64, u8, i32, f64::powi);
|
|
|
|
pow_impl!(f64, i16, i32, f64::powi);
|
|
|
|
pow_impl!(f64, u16, i32, f64::powi);
|
|
|
|
pow_impl!(f64, i32, i32, f64::powi);
|
|
|
|
pow_impl!(f32, f32, f32, f32::powf);
|
|
|
|
pow_impl!(f64, f32, f64, f64::powf);
|
|
|
|
pow_impl!(f64, f64, f64, f64::powf);
|
2018-02-19 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:02:21 +00:00
|
|
|
/// Raises a value to the power of exp, using exponentiation by squaring.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use num_traits::pow;
|
|
|
|
///
|
|
|
|
/// assert_eq!(pow(2i8, 4), 16);
|
|
|
|
/// assert_eq!(pow(6u8, 3), 216);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
|
2018-07-13 00:09:22 +00:00
|
|
|
if exp == 0 {
|
|
|
|
return T::one();
|
|
|
|
}
|
2016-05-09 15:02:21 +00:00
|
|
|
|
|
|
|
while exp & 1 == 0 {
|
|
|
|
base = base.clone() * base;
|
|
|
|
exp >>= 1;
|
|
|
|
}
|
2018-07-13 00:09:22 +00:00
|
|
|
if exp == 1 {
|
|
|
|
return base;
|
|
|
|
}
|
2016-05-09 15:02:21 +00:00
|
|
|
|
|
|
|
let mut acc = base.clone();
|
|
|
|
while exp > 1 {
|
|
|
|
exp >>= 1;
|
|
|
|
base = base.clone() * base;
|
|
|
|
if exp & 1 == 1 {
|
|
|
|
acc = acc * base.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Raises a value to the power of exp, returning `None` if an overflow occurred.
|
|
|
|
///
|
|
|
|
/// Otherwise same as the `pow` function.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use num_traits::checked_pow;
|
|
|
|
///
|
|
|
|
/// assert_eq!(checked_pow(2i8, 4), Some(16));
|
|
|
|
/// assert_eq!(checked_pow(7i8, 8), None);
|
|
|
|
/// assert_eq!(checked_pow(7u32, 8), Some(5_764_801));
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> {
|
2018-07-13 00:09:22 +00:00
|
|
|
if exp == 0 {
|
|
|
|
return Some(T::one());
|
|
|
|
}
|
2016-05-09 15:02:21 +00:00
|
|
|
|
|
|
|
macro_rules! optry {
|
2018-07-13 00:09:22 +00:00
|
|
|
($expr:expr) => {
|
|
|
|
if let Some(val) = $expr {
|
|
|
|
val
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2016-05-09 15:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while exp & 1 == 0 {
|
|
|
|
base = optry!(base.checked_mul(&base));
|
|
|
|
exp >>= 1;
|
|
|
|
}
|
2018-07-13 00:09:22 +00:00
|
|
|
if exp == 1 {
|
|
|
|
return Some(base);
|
|
|
|
}
|
2016-05-09 15:02:21 +00:00
|
|
|
|
|
|
|
let mut acc = base.clone();
|
|
|
|
while exp > 1 {
|
|
|
|
exp >>= 1;
|
|
|
|
base = optry!(base.checked_mul(&base));
|
|
|
|
if exp & 1 == 1 {
|
|
|
|
acc = optry!(acc.checked_mul(&base));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(acc)
|
|
|
|
}
|