Skip to content

Replace old pow_with_uint with the new pow func #11649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl<T : Iterator<char>> Parser<T> {
}
}

let exp: f64 = num::pow_with_uint(10u, exp);
let exp: f64 = num::pow(10u as f64, exp);
if neg_exp {
res /= exp;
} else {
Expand Down
46 changes: 15 additions & 31 deletions src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,37 +993,6 @@ pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
FromStrRadix::from_str_radix(str, radix)
}

/// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
///
/// Returns `radix^pow` as `T`.
///
/// Note:
/// Also returns `1` for `0^0`, despite that technically being an
/// undefined number. The reason for this is twofold:
/// - If code written to use this function cares about that special case, it's
/// probably going to catch it before making the call.
/// - If code written to use this function doesn't care about it, it's
/// probably assuming that `x^0` always equals `1`.
///
pub fn pow_with_uint<T:NumCast+One+Zero+Div<T,T>+Mul<T,T>>(radix: uint, pow: uint) -> T {
let _0: T = Zero::zero();
let _1: T = One::one();

if pow == 0u { return _1; }
if radix == 0u { return _0; }
let mut my_pow = pow;
let mut total = _1;
let mut multiplier = cast(radix).unwrap();
while (my_pow > 0u) {
if my_pow % 2u == 1u {
total = total * multiplier;
}
my_pow = my_pow / 2u;
multiplier = multiplier * multiplier;
}
total
}

impl<T: Zero + 'static> Zero for @T {
fn zero() -> @T { @Zero::zero() }
fn is_zero(&self) -> bool { (**self).is_zero() }
Expand Down Expand Up @@ -1684,3 +1653,18 @@ mod tests {
assert_pow(2u64, 50);
}
}


#[cfg(test)]
mod bench {
use num;
use vec;
use prelude::*;
use extra::test::BenchHarness;

#[bench]
fn bench_pow_function(b: &mut BenchHarness) {
let v = vec::from_fn(1024, |n| n);
b.iter(|| {v.iter().fold(0, |old, new| num::pow(old, *new));});
}
}
13 changes: 7 additions & 6 deletions src/libstd/num/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use str::{StrSlice};
use str;
use vec::{CopyableVector, ImmutableVector, MutableVector};
use vec::OwnedVector;
use num::{NumCast, Zero, One, cast, pow_with_uint, Integer};
use num;
use num::{NumCast, Zero, One, cast, Integer};
use num::{Round, Float, FPNaN, FPInfinite, ToPrimitive};

pub enum ExponentFormat {
Expand Down Expand Up @@ -648,10 +649,10 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+

if exp_found {
let c = buf[i] as char;
let base = match (c, exponent) {
let base: T = match (c, exponent) {
// c is never _ so don't need to handle specially
('e', ExpDec) | ('E', ExpDec) => 10u,
('p', ExpBin) | ('P', ExpBin) => 2u,
('e', ExpDec) | ('E', ExpDec) => cast(10u).unwrap(),
('p', ExpBin) | ('P', ExpBin) => cast(2u).unwrap(),
_ => return None // char doesn't fit given exponent format
};

Expand All @@ -664,9 +665,9 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Div<T,T>+
match exp {
Some(exp_pow) => {
multiplier = if exp_pow < 0 {
_1 / pow_with_uint::<T>(base, (-exp_pow.to_int().unwrap()) as uint)
_1 / num::pow(base, (-exp_pow.to_int().unwrap()) as uint)
} else {
pow_with_uint::<T>(base, exp_pow.to_int().unwrap() as uint)
num::pow(base, exp_pow.to_int().unwrap() as uint)
}
}
None => return None // invalid exponent -> invalid number
Expand Down