Skip to content

Commit d7a29d8

Browse files
committed
auto merge of #19031 : nodakai/rust/libcore-pow-and-sq, r=bjz
[breaking-change] Deprecates `core::num::pow` in favor of `Int::pow`.
2 parents 516ece6 + 3fcf284 commit d7a29d8

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

src/libcore/num/mod.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,10 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
3939
}
4040

4141
/// Raises a `base` to the power of `exp`, using exponentiation by squaring.
42-
///
43-
/// # Example
44-
///
45-
/// ```rust
46-
/// use std::num;
47-
///
48-
/// assert_eq!(num::pow(2i, 4), 16);
49-
/// ```
5042
#[inline]
51-
pub fn pow<T: Int>(mut base: T, mut exp: uint) -> T {
52-
if exp == 1 { base }
53-
else {
54-
let mut acc: T = Int::one();
55-
while exp > 0 {
56-
if (exp & 1) == 1 {
57-
acc = acc * base;
58-
}
59-
base = base * base;
60-
exp = exp >> 1;
61-
}
62-
acc
63-
}
43+
#[deprecated = "Use Int::pow() instead, as in 2i.pow(4)"]
44+
pub fn pow<T: Int>(base: T, exp: uint) -> T {
45+
base.pow(exp)
6446
}
6547

6648
/// A built-in signed or unsigned integer.
@@ -361,6 +343,29 @@ pub trait Int
361343
None => Int::max_value(),
362344
}
363345
}
346+
347+
/// Raises self to the power of `exp`, using exponentiation by squaring.
348+
///
349+
/// # Example
350+
///
351+
/// ```rust
352+
/// use std::num::Int;
353+
///
354+
/// assert_eq!(2i.pow(4), 16);
355+
/// ```
356+
#[inline]
357+
fn pow(self, mut exp: uint) -> Self {
358+
let mut base = self;
359+
let mut acc: Self = Int::one();
360+
while exp > 0 {
361+
if (exp & 1) == 1 {
362+
acc = acc * base;
363+
}
364+
base = base * base;
365+
exp /= 2;
366+
}
367+
acc
368+
}
364369
}
365370

366371
macro_rules! checked_op {

src/libstd/num/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ mod tests {
757757
}
758758
macro_rules! assert_pow(
759759
(($num:expr, $exp:expr) => $expected:expr) => {{
760-
let result = pow($num, $exp);
760+
let result = $num.pow($exp);
761761
assert_eq!(result, $expected);
762762
assert_eq!(result, naive_pow($num, $exp));
763763
}}
@@ -775,12 +775,12 @@ mod tests {
775775
mod bench {
776776
extern crate test;
777777
use self::test::Bencher;
778-
use num;
778+
use num::Int;
779779
use prelude::*;
780780

781781
#[bench]
782782
fn bench_pow_function(b: &mut Bencher) {
783783
let v = Vec::from_fn(1024u, |n| n);
784-
b.iter(|| {v.iter().fold(0u, |old, new| num::pow(old, *new));});
784+
b.iter(|| {v.iter().fold(0u, |old, new| old.pow(*new));});
785785
}
786786
}

src/test/bench/shootout-binarytrees.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ fn main() {
9393
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
9494

9595
let mut messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
96-
use std::num::pow;
97-
let iterations = pow(2i, (max_depth - depth + min_depth) as uint);
96+
use std::num::Int;
97+
let iterations = 2i.pow((max_depth - depth + min_depth) as uint);
9898
Future::spawn(proc() {
9999
let mut chk = 0;
100100
for i in range(1, iterations + 1) {

src/test/compile-fail/lint-dead-code-4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
extern crate libc;
1616

17-
use std::num;
17+
use std::num::Int;
1818

1919
struct Foo {
2020
x: uint,
@@ -23,7 +23,7 @@ struct Foo {
2323
}
2424

2525
fn field_read(f: Foo) -> uint {
26-
num::pow(f.x, 2)
26+
f.x.pow(2)
2727
}
2828

2929
enum XYZ {

0 commit comments

Comments
 (0)