From 3fcf2840a484159c9e27601dc9480f9636d2f2e5 Mon Sep 17 00:00:00 2001 From: "NODA, Kai" Date: Mon, 17 Nov 2014 22:12:54 +0800 Subject: libcore: add num::Int::pow() and deprecate num::pow(). Signed-off-by: NODA, Kai --- src/libcore/num/mod.rs | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'src/libcore/num') diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index f5505ff8e76..df8538fe3f2 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -37,28 +37,10 @@ pub fn div_rem + Rem>(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(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(base: T, exp: uint) -> T { + base.pow(exp) } /// A built-in signed or unsigned integer. @@ -359,6 +341,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 { -- cgit 1.4.1-3-g733a5