summary refs log tree commit diff
path: root/src/libcore/num
diff options
context:
space:
mode:
authorNODA, Kai <nodakai@gmail.com>2014-11-17 22:12:54 +0800
committerNODA, Kai <nodakai@gmail.com>2014-11-18 10:42:27 +0800
commit3fcf2840a484159c9e27601dc9480f9636d2f2e5 (patch)
treed5533bf566c33b4c7eac0517c69bba186f05433a /src/libcore/num
parent803aacd5aef78f90fdd06ae7653fc20eec224992 (diff)
downloadrust-3fcf2840a484159c9e27601dc9480f9636d2f2e5.tar.gz
rust-3fcf2840a484159c9e27601dc9480f9636d2f2e5.zip
libcore: add num::Int::pow() and deprecate num::pow().
Signed-off-by: NODA, Kai <nodakai@gmail.com>
Diffstat (limited to 'src/libcore/num')
-rw-r--r--src/libcore/num/mod.rs47
1 files changed, 26 insertions, 21 deletions
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<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.
@@ -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 {