pow.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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(feature = "std")]
  143. mod float_impls {
  144. use super::Pow;
  145. pow_impl!(f32, i8, i32, f32::powi);
  146. pow_impl!(f32, u8, i32, f32::powi);
  147. pow_impl!(f32, i16, i32, f32::powi);
  148. pow_impl!(f32, u16, i32, f32::powi);
  149. pow_impl!(f32, i32, i32, f32::powi);
  150. pow_impl!(f64, i8, i32, f64::powi);
  151. pow_impl!(f64, u8, i32, f64::powi);
  152. pow_impl!(f64, i16, i32, f64::powi);
  153. pow_impl!(f64, u16, i32, f64::powi);
  154. pow_impl!(f64, i32, i32, f64::powi);
  155. pow_impl!(f32, f32, f32, f32::powf);
  156. pow_impl!(f64, f32, f64, f64::powf);
  157. pow_impl!(f64, f64, f64, f64::powf);
  158. }
  159. /// Raises a value to the power of exp, using exponentiation by squaring.
  160. ///
  161. /// Note that `0⁰` (`pow(0, 0)`) returnes `1`. Mathematically this is undefined.
  162. ///
  163. /// # Example
  164. ///
  165. /// ```rust
  166. /// use num_traits::pow;
  167. ///
  168. /// assert_eq!(pow(2i8, 4), 16);
  169. /// assert_eq!(pow(6u8, 3), 216);
  170. /// assert_eq!(pow(0u8, 0), 1); // Be aware if this case affects you
  171. /// ```
  172. #[inline]
  173. pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
  174. if exp == 0 {
  175. return T::one();
  176. }
  177. while exp & 1 == 0 {
  178. base = base.clone() * base;
  179. exp >>= 1;
  180. }
  181. if exp == 1 {
  182. return base;
  183. }
  184. let mut acc = base.clone();
  185. while exp > 1 {
  186. exp >>= 1;
  187. base = base.clone() * base;
  188. if exp & 1 == 1 {
  189. acc = acc * base.clone();
  190. }
  191. }
  192. acc
  193. }
  194. /// Raises a value to the power of exp, returning `None` if an overflow occurred.
  195. ///
  196. /// Note that `0⁰` (`checked_pow(0, 0)`) returnes `Some(1)`. Mathematically this is undefined.
  197. ///
  198. /// Otherwise same as the `pow` function.
  199. ///
  200. /// # Example
  201. ///
  202. /// ```rust
  203. /// use num_traits::checked_pow;
  204. ///
  205. /// assert_eq!(checked_pow(2i8, 4), Some(16));
  206. /// assert_eq!(checked_pow(7i8, 8), None);
  207. /// assert_eq!(checked_pow(7u32, 8), Some(5_764_801));
  208. /// assert_eq!(checked_pow(0u32, 0), Some(1)); // Be aware if this case affect you
  209. /// ```
  210. #[inline]
  211. pub fn checked_pow<T: Clone + One + CheckedMul>(mut base: T, mut exp: usize) -> Option<T> {
  212. if exp == 0 {
  213. return Some(T::one());
  214. }
  215. macro_rules! optry {
  216. ($expr:expr) => {
  217. if let Some(val) = $expr {
  218. val
  219. } else {
  220. return None;
  221. }
  222. };
  223. }
  224. while exp & 1 == 0 {
  225. base = optry!(base.checked_mul(&base));
  226. exp >>= 1;
  227. }
  228. if exp == 1 {
  229. return Some(base);
  230. }
  231. let mut acc = base.clone();
  232. while exp > 1 {
  233. exp >>= 1;
  234. base = optry!(base.checked_mul(&base));
  235. if exp & 1 == 1 {
  236. acc = optry!(acc.checked_mul(&base));
  237. }
  238. }
  239. Some(acc)
  240. }