Skip to content
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

Various improvements to FloatCore #41

Merged
merged 15 commits into from
Mar 1, 2018
Merged
6 changes: 3 additions & 3 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::num::FpCategory;
use core::f32;
use core::f64;

use {Num, NumCast, ToPrimitive};
use {Num, NumCast};

/// Generic trait for floating point numbers that works with `no_std`.
///
Expand Down Expand Up @@ -218,11 +218,11 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
#[inline]
fn powi(mut self, mut exp: i32) -> Self {
if exp < 0 {
exp = -exp;
exp = exp.wrapping_neg();
self = self.recip();
}
// It should always be possible to convert a positive `i32` to a `usize`.
super::pow(self, exp.to_usize().unwrap())
super::pow(self, exp as u32 as usize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To handle i32::MIN. See above that I switched it to wrapping_neg, which will leave MIN the same. We want that to become an unsigned positive 0x80000000, but to_usize() would just fail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I compromised -- added a comment why the as u32 is needed, but went back to using to_usize() so we won't be surprised here by a platform with 16-bit usize (although that will panic).

}

/// Converts to degrees, assuming the number is in radians.
Expand Down