diff options
| author | bors <bors@rust-lang.org> | 2014-11-18 13:41:38 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-11-18 13:41:38 +0000 |
| commit | d7a29d87ba6bfca0bfe6207a99c7ec2b7ce869b9 (patch) | |
| tree | d5ef76ecd61bb50bb094b01423893d1f49b42260 /src/libcore/num | |
| parent | 516ece6ee4393990b8a62fb8f16bb7423e0e8828 (diff) | |
| parent | 3fcf2840a484159c9e27601dc9480f9636d2f2e5 (diff) | |
| download | rust-d7a29d87ba6bfca0bfe6207a99c7ec2b7ce869b9.tar.gz rust-d7a29d87ba6bfca0bfe6207a99c7ec2b7ce869b9.zip | |
auto merge of #19031 : nodakai/rust/libcore-pow-and-sq, r=bjz
[breaking-change] Deprecates `core::num::pow` in favor of `Int::pow`.
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/mod.rs | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index a794897ce6b..04ca2c6001b 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -39,28 +39,10 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) { } /// Raises a `base` to the power of `exp`, using exponentiation by squaring. -/// -/// # Example -/// -/// ```rust -/// use std::num; -/// -/// assert_eq!(num::pow(2i, 4), 16); -/// ``` #[inline] -pub fn pow<T: Int>(mut base: T, mut exp: uint) -> T { - if exp == 1 { base } - else { - let mut acc: T = Int::one(); - while exp > 0 { - if (exp & 1) == 1 { - acc = acc * base; - } - base = base * base; - exp = exp >> 1; - } - acc - } +#[deprecated = "Use Int::pow() instead, as in 2i.pow(4)"] +pub fn pow<T: Int>(base: T, exp: uint) -> T { + base.pow(exp) } /// A built-in signed or unsigned integer. @@ -361,6 +343,29 @@ pub trait Int None => Int::max_value(), } } + + /// Raises self to the power of `exp`, using exponentiation by squaring. + /// + /// # Example + /// + /// ```rust + /// use std::num::Int; + /// + /// assert_eq!(2i.pow(4), 16); + /// ``` + #[inline] + fn pow(self, mut exp: uint) -> Self { + let mut base = self; + let mut acc: Self = Int::one(); + while exp > 0 { + if (exp & 1) == 1 { + acc = acc * base; + } + base = base * base; + exp /= 2; + } + acc + } } macro_rules! checked_op { |
