Don't duplicate `num_traits::pow`

This commit is contained in:
Vinzent Steinberg 2017-06-07 12:17:16 +02:00
parent f8a61962ba
commit 63ce0f04ae
1 changed files with 5 additions and 24 deletions

View File

@ -5,7 +5,7 @@ use core::num::FpCategory;
// Used for default implementation of `epsilon` // Used for default implementation of `epsilon`
use core::f32; use core::f32;
use {Num, NumCast}; use {ToPrimitive, Num, NumCast};
// FIXME: these doctests aren't actually helpful, because they're using and // FIXME: these doctests aren't actually helpful, because they're using and
// testing the inherent methods directly, not going through `Float`. // testing the inherent methods directly, not going through `Float`.
@ -456,32 +456,13 @@ pub trait Float
/// assert!(abs_difference < 1e-10); /// assert!(abs_difference < 1e-10);
/// ``` /// ```
#[inline] #[inline]
fn powi(self, mut exp: i32) -> Self { fn powi(mut self, mut exp: i32) -> Self {
if exp == 0 { return Self::one() }
let mut base;
if exp < 0 { if exp < 0 {
self = self.recip();
exp = -exp; exp = -exp;
base = self.recip();
} else {
base = self;
} }
// It should always be possible to convert a positive `i32` to a `usize`.
while exp & 1 == 0 { super::pow(self, exp.to_usize().unwrap())
base = base * base;
exp >>= 1;
}
if exp == 1 { return base }
let mut acc = base;
while exp > 1 {
exp >>= 1;
base = base * base;
if exp & 1 == 1 {
acc = acc * base;
}
}
acc
} }
/// Raise a number to a floating point power. /// Raise a number to a floating point power.