pow.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. use core::num::Wrapping;
  2. use core::ops::Mul;
  3. use {CheckedMul, One};
  4. /// Binary operator for raising a value to a power.
  5. pub trait Pow<RHS> {
  6. /// The result after applying the operator.
  7. type Output;
  8. /// Returns `self` to the power `rhs`.
  9. ///
  10. /// # Examples
  11. ///
  12. /// ```
  13. /// use num_traits::Pow;
  14. /// assert_eq!(Pow::pow(10u32, 2u32), 100);
  15. /// ```
  16. fn pow(self, rhs: RHS) -> Self::Output;
  17. }
  18. macro_rules! pow_impl {
  19. ($t:ty) => {
  20. pow_impl!($t, u8);
  21. pow_impl!($t, usize);
  22. // FIXME: these should be possible
  23. // pow_impl!($t, u16);
  24. // pow_impl!($t, u32);
  25. // pow_impl!($t, u64);
  26. };
  27. ($t:ty, $rhs:ty) => {
  28. pow_impl!($t, $rhs, usize, pow);
  29. };
  30. ($t:ty, $rhs:ty, $desired_rhs:ty, $method:expr) => {
  31. impl Pow<$rhs> for $t {
  32. type Output = $t;
  33. #[inline]
  34. fn pow(self, rhs: $rhs) -> $t {
  35. ($method)(self, <$desired_rhs>::from(rhs))
  36. }
  37. }
  38. impl<'a> Pow<&'a $rhs> for $t {
  39. type Output = $t;
  40. #[inline]
  41. fn pow(self, rhs: &'a $rhs) -> $t {
  42. ($method)(self, <$desired_rhs>::from(*rhs))
  43. }
  44. }
  45. impl<'a> Pow<$rhs> for &'a $t {
  46. type Output = $t;
  47. #[inline]
  48. fn pow(self, rhs: $rhs) -> $t {
  49. ($method)(*self, <$desired_rhs>::from(rhs))
  50. }
  51. }
  52. impl<'a, 'b> Pow<&'a $rhs> for &'b $t {
  53. type Output = $t;
  54. #[inline]
  55. fn pow(self, rhs: &'a $rhs) -> $t {
  56. ($method)(*self, <$desired_rhs>::from(*rhs))
  57. }
  58. }
  59. };
  60. }
  61. pow_impl!(u8, u8, u32, u8::pow);
  62. pow_impl!(u8, u16, u32, u8::pow);
  63. pow_impl!(u8, u32, u32, u8::pow);
  64. pow_impl!(u8, usize);
  65. pow_impl!(i8, u8, u32, i8::pow);
  66. pow_impl!(i8, u16, u32, i8::pow);
  67. pow_impl!(i8, u32, u32, i8::pow);
  68. pow_impl!(i8, usize);
  69. pow_impl!(u16, u8, u32, u16::pow);
  70. pow_impl!(u16, u16, u32, u16::pow);
  71. pow_impl!(u16, u32, u32, u16::pow);
  72. pow_impl!(u16, usize);
  73. pow_impl!(i16, u8, u32, i16::pow);
  74. pow_impl!(i16, u16, u32, i16::pow);
  75. pow_impl!(i16, u32, u32, i16::pow);
  76. pow_impl!(i16, usize);
  77. pow_impl!(u32, u8, u32, u32::pow);
  78. pow_impl!(u32, u16, u32, u32::pow);
  79. pow_impl!(u32, u32, u32, u32::pow);
  80. pow_impl!(u32, usize);
  81. pow_impl!(i32, u8, u32, i32::pow);
  82. pow_impl!(i32, u16, u32, i32::pow);
  83. pow_impl!(i32, u32, u32, i32::pow);
  84. pow_impl!(i32, usize);
  85. pow_impl!(u64, u8, u32, u64::pow);
  86. pow_impl!(u64, u16, u32, u64::pow);
  87. pow_impl!(u64, u32, u32, u64::pow);
  88. pow_impl!(u64, usize);
  89. pow_impl!(i64, u8, u32, i64::pow);
  90. pow_impl!(i64, u16, u32, i64::pow);
  91. pow_impl!(i64, u32, u32, i64::pow);
  92. pow_impl!(i64, usize);
  93. #[cfg(has_i128)]
  94. pow_impl!(u128, u8, u32, u128::pow);
  95. #[cfg(has_i128)]
  96. pow_impl!(u128, u16, u32, u128::pow);
  97. #[cfg(has_i128)]
  98. pow_impl!(u128, u32, u32, u128::pow);
  99. #[cfg(has_i128)]
  100. pow_impl!(u128, usize);
  101. #[cfg(has_i128)]
  102. pow_impl!(i128, u8, u32, i128::pow);
  103. #[cfg(has_i128)]
  104. pow_impl!(i128, u16, u32, i128::pow);
  105. #[cfg(has_i128)]
  106. pow_impl!(i128, u32, u32, i128::pow);
  107. #[cfg(has_i128)]
  108. pow_impl!(i128, usize);
  109. pow_impl!(usize, u8, u32, usize::pow);
  110. pow_impl!(usize, u16, u32, usize::pow);
  111. pow_impl!(usize, u32, u32, usize::pow);
  112. pow_impl!(usize, usize);
  113. pow_impl!(isize, u8, u32, isize::pow);
  114. pow_impl!(isize, u16, u32, isize::pow);
  115. pow_impl!(isize, u32, u32, isize::pow);
  116. pow_impl!(isize, usize);
  117. pow_impl!(Wrapping<u8>);
  118. pow_impl!(Wrapping<i8>);
  119. pow_impl!(Wrapping<u16>);
  120. pow_impl!(Wrapping<i16>);
  121. pow_impl!(Wrapping<u32>);
  122. pow_impl!(Wrapping<i32>);
  123. pow_impl!(Wrapping<u64>);
  124. pow_impl!(Wrapping<i64>);
  125. #[cfg(has_i128)]
  126. pow_impl!(Wrapping<u128>);
  127. #[cfg(has_i128)]
  128. pow_impl!(Wrapping<i128>);
  129. pow_impl!(Wrapping<usize>);
  130. pow_impl!(Wrapping<isize>);
  131. // FIXME: these should be possible
  132. // pow_impl!(u8, u64);
  133. // pow_impl!(i16, u64);
  134. // pow_impl!(i8, u64);
  135. // pow_impl!(u16, u64);
  136. // pow_impl!(u32, u64);
  137. // pow_impl!(i32, u64);
  138. // pow_impl!(u64, u64);
  139. // pow_impl!(i64, u64);
  140. // pow_impl!(usize, u64);
  141. // pow_impl!(isize, u64);
  142. #[cfg(any(feature = "std", feature = "libm"))]
  143. mod float_impls {
  144. use super::Pow;
  145. use Float;
  146. pow_impl!(f32, i8, i32, <f32 as Float>::powi);
  147. pow_impl!(f32, u8, i32, <f32 as Float>::powi);
  148. pow_impl!(f32, i16, i32, <f32 as Float>::powi);
  149. pow_impl!(f32, u16, i32, <f32 as Float>::powi);
  150. pow_impl!(f32, i32, i32, <f32 as Float>::powi);
  151. pow_impl!(f64, i8, i32, <f64 as Float>::powi);
  152. pow_impl!(f64, u8, i32, <f64 as Float>::powi);
  153. pow_impl!(f64, i16, i32, <f64 as Float>::powi);
  154. pow_impl!(f64, u16, i32, <f64 as Float>::powi);
  155. pow_impl!(f64, i32, i32, <f64 as Float>::powi);
  156. pow_impl!(f32, f32, f32, <f32 as Float>::powf);
  157. pow_impl!(f64, f32, f64, <f64 as Float>::powf);
  158. pow_impl!(f64, f64, f64, <f64 as Float>::powf);
  159. }
  160. /// Raises a value to the power of exp, using exponentiation by squaring.
  161. ///
  162. /// Note that `0⁰` (`pow(0, 0)`) returns `1`. Mathematically this is undefined.
  163. ///
  164. /// # Example
  165. ///
  166. /// ```rust
  167. /// use num_traits::pow;
  168. ///
  169. /// assert_eq!(pow(2i8, 4), 16);
  170. /// assert_eq!(pow(6u8, 3), 216);
  171. /// assert_eq!(pow(0u8, 0), 1); // Be aware if this case affects you
  172. /// ```
  173. #[inline]
  174. pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
  175. if exp == 0 {
  176. return T::one();
  177. }
  178. while exp & 1 == 0 {
  179. base = base.clone() * base;
  180. exp >>= 1;
  181. }
  182. if exp == 1 {
  183. return base;
  184. }
  185. let mut acc = base.clone();
  186. while exp > 1 {
  187. exp >>= 1;
  188. base = base.clone() * base;
  189. if exp & 1 == 1 {
  190. acc = acc * base.clone();
  191. }
  192. }
  193. acc
  194. }
  195. /// Raises a value to the power of exp, returning `None` if an overflow occurred.
  196. ///
  197. /// Note that `0⁰` (`checked_pow(0, 0)`) returns `Some(1)`. Mathematically this is undefined.
  198. ///
  199. /// Otherwise same as the `pow` function.
  200. ///
  201. /// # Example
  202. ///
  203. /// ```rust
  204. /// use num_traits::checked_pow;
  205. ///
  206. /// assert_eq!(checked_pow(2i8, 4), Some(16));
  207. /// assert_eq!(checked_pow(7i8, 8), None);
  208. /// assert_eq!(checked_pow(7u32, 8), Some(5_764_801));
  209. /// assert_eq!(checked_pow(0u32, 0), Some(1)); // Be aware if this case affect you
  210. /// ```
  211. #[inline]
  212. pub fn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> {
  213. if exp == 0 {
  214. return Some(T::one());
  215. }
  216. macro_rules! optry {
  217. ($expr:expr) => {
  218. if let Some(val) = $expr {
  219. val
  220. } else {
  221. return None;
  222. }
  223. };
  224. }
  225. while exp & 1 == 0 {
  226. base = optry!(base.checked_mul(&base));
  227. exp >>= 1;
  228. }
  229. if exp == 1 {
  230. return Some(base);
  231. }
  232. let mut acc = base.clone();
  233. while exp > 1 {
  234. exp >>= 1;
  235. base = optry!(base.checked_mul(&base));
  236. if exp & 1 == 1 {
  237. acc = optry!(acc.checked_mul(&base));
  238. }
  239. }
  240. Some(acc)
  241. }