From de938b6ca1a2b6b6df65d5935c765a7c25fbce84 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 13 Nov 2014 00:02:42 +1100 Subject: Remove Signed trait and add SignedInt trait The methods have been moved into Float and SignedInt --- src/libcore/num/f32.rs | 36 ++++++++ src/libcore/num/f64.rs | 35 ++++++++ src/libcore/num/float_macros.rs | 1 + src/libcore/num/mod.rs | 190 ++++++++++++++++------------------------ 4 files changed, 146 insertions(+), 116 deletions(-) (limited to 'src/libcore/num') diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 5913b8d75a7..ba03bb8f3d5 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -242,6 +242,41 @@ impl Float for f32 { #[inline] fn fract(self) -> f32 { self - self.trunc() } + /// Computes the absolute value of `self`. Returns `Float::nan()` if the + /// number is `Float::nan()`. + #[inline] + fn abs(self) -> f32 { + unsafe { intrinsics::fabsf32(self) } + } + + /// Returns a number that represents the sign of `self`. + /// + /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` + /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` + /// - `Float::nan()` if the number is `Float::nan()` + #[inline] + fn signum(self) -> f32 { + if self.is_nan() { + Float::nan() + } else { + unsafe { intrinsics::copysignf32(1.0, self) } + } + } + + /// Returns `true` if `self` is positive, including `+0.0` and + /// `Float::infinity()`. + #[inline] + fn is_positive(self) -> bool { + self > 0.0 || (1.0 / self) == Float::infinity() + } + + /// Returns `true` if `self` is negative, including `-0.0` and + /// `Float::neg_infinity()`. + #[inline] + fn is_negative(self) -> bool { + self < 0.0 || (1.0 / self) == Float::neg_infinity() + } + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding /// error. This produces a more accurate result with better performance than /// a separate multiplication operation followed by an add. @@ -254,6 +289,7 @@ impl Float for f32 { #[inline] fn recip(self) -> f32 { 1.0 / self } + #[inline] fn powi(self, n: i32) -> f32 { unsafe { intrinsics::powif32(self, n) } } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 8f68578d768..f1af4f0272c 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -248,6 +248,41 @@ impl Float for f64 { #[inline] fn fract(self) -> f64 { self - self.trunc() } + /// Computes the absolute value of `self`. Returns `Float::nan()` if the + /// number is `Float::nan()`. + #[inline] + fn abs(self) -> f64 { + unsafe { intrinsics::fabsf64(self) } + } + + /// Returns a number that represents the sign of `self`. + /// + /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` + /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` + /// - `Float::nan()` if the number is `Float::nan()` + #[inline] + fn signum(self) -> f64 { + if self.is_nan() { + Float::nan() + } else { + unsafe { intrinsics::copysignf64(1.0, self) } + } + } + + /// Returns `true` if `self` is positive, including `+0.0` and + /// `Float::infinity()`. + #[inline] + fn is_positive(self) -> bool { + self > 0.0 || (1.0 / self) == Float::infinity() + } + + /// Returns `true` if `self` is negative, including `-0.0` and + /// `Float::neg_infinity()`. + #[inline] + fn is_negative(self) -> bool { + self < 0.0 || (1.0 / self) == Float::neg_infinity() + } + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding /// error. This produces a more accurate result with better performance than /// a separate multiplication operation followed by an add. diff --git a/src/libcore/num/float_macros.rs b/src/libcore/num/float_macros.rs index 3e403219a4f..d15cff3a8a9 100644 --- a/src/libcore/num/float_macros.rs +++ b/src/libcore/num/float_macros.rs @@ -13,6 +13,7 @@ macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ + use num::Float; let (a, b) = (&$a, &$b); assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b); diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index dd2ee6e01c6..216d140ac48 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -33,113 +33,6 @@ pub fn div_rem + Rem>(x: T, y: T) -> (T, T) { (x / y, x % y) } -/// A built-in signed number. -pub trait Signed: Neg { - /// Computes the absolute value of `self`. - fn abs(self) -> Self; - - /// Returns a number (either `-1`, `0` or `1`) representing sign of `self`. - fn signum(self) -> Self; - - /// Returns `true` if `self` is a positive value. - fn is_positive(self) -> bool; - - /// Returns `true` if `self` is a negative value. - fn is_negative(self) -> bool; -} - -macro_rules! signed_int_impl { - ($T:ty) => { - impl Signed for $T { - /// Computes the absolute value. `Int::min_value()` will be returned - /// if the number is `Int::min_value()`. - #[inline] - fn abs(self) -> $T { - if self.is_negative() { -self } else { self } - } - - /// # Returns - /// - /// - `0` if the number is zero - /// - `1` if the number is positive - /// - `-1` if the number is negative - #[inline] - fn signum(self) -> $T { - match self { - n if n > 0 => 1, - 0 => 0, - _ => -1, - } - } - - /// Returns `true` if `self` is positive and `false` if the number - /// is zero or negative. - #[inline] - fn is_positive(self) -> bool { self > 0 } - - /// Returns `true` if `self` is negative and `false` if the number - /// is zero or positive. - #[inline] - fn is_negative(self) -> bool { self < 0 } - } - } -} - -signed_int_impl!(i8) -signed_int_impl!(i16) -signed_int_impl!(i32) -signed_int_impl!(i64) -signed_int_impl!(int) - -macro_rules! signed_float_impl { - ($T:ty, $fabs:path, $fcopysign:path) => { - impl Signed for $T { - /// Computes the absolute value. Returns `Float::nan()` if the - /// number is `Float::nan()`. - #[inline] - fn abs(self) -> $T { - unsafe { $fabs(self) } - } - - /// # Returns - /// - /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` - /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` - /// - `Float::nan()` if the number is `Float::nan()` - #[inline] - fn signum(self) -> $T { - if self.is_nan() { - Float::nan() - } else { - unsafe { $fcopysign(1.0, self) } - } - } - - /// Returns `true` if the number is positive, including `+0.0` and - /// `Float::infinity()`. - #[inline] - fn is_positive(self) -> bool { - self > 0.0 || (1.0 / self) == Float::infinity() - } - - /// Returns `true` if the number is negative, including `-0.0` and - /// `Float::neg_infinity()`. - #[inline] - fn is_negative(self) -> bool { - self < 0.0 || (1.0 / self) == Float::neg_infinity() - } - } - }; -} - -signed_float_impl!(f32, - intrinsics::fabsf32, - intrinsics::copysignf32) - -signed_float_impl!(f64, - intrinsics::fabsf64, - intrinsics::copysignf64) - /// Raises a `base` to the power of `exp`, using exponentiation by squaring. /// /// # Example @@ -702,6 +595,63 @@ int_impl!(int = i64, u64, 64, intrinsics::i64_sub_with_overflow, intrinsics::i64_mul_with_overflow) +/// A built-in two's complement integer. +pub trait SignedInt + : Int + + Neg +{ + /// Computes the absolute value of `self`. `Int::min_value()` will be + /// returned if the number is `Int::min_value()`. + fn abs(self) -> Self; + + /// Returns a number representing sign of `self`. + /// + /// - `0` if the number is zero + /// - `1` if the number is positive + /// - `-1` if the number is negative + fn signum(self) -> Self; + + /// Returns `true` if `self` is positive and `false` if the number + /// is zero or negative. + fn is_positive(self) -> bool; + + /// Returns `true` if `self` is negative and `false` if the number + /// is zero or positive. + fn is_negative(self) -> bool; +} + +macro_rules! signed_int_impl { + ($T:ty) => { + impl SignedInt for $T { + #[inline] + fn abs(self) -> $T { + if self.is_negative() { -self } else { self } + } + + #[inline] + fn signum(self) -> $T { + match self { + n if n > 0 => 1, + 0 => 0, + _ => -1, + } + } + + #[inline] + fn is_positive(self) -> bool { self > 0 } + + #[inline] + fn is_negative(self) -> bool { self < 0 } + } + } +} + +signed_int_impl!(i8) +signed_int_impl!(i16) +signed_int_impl!(i32) +signed_int_impl!(i64) +signed_int_impl!(int) + /// A built-in unsigned integer. pub trait UnsignedInt: Int { /// Returns `true` iff `self == 2^k` for some `k`. @@ -1257,7 +1207,7 @@ pub trait Float + NumCast + PartialOrd + PartialEq - + Signed + + Neg + Add + Sub + Mul @@ -1327,6 +1277,22 @@ pub trait Float /// Return the fractional part of a number. fn fract(self) -> Self; + /// Computes the absolute value of `self`. Returns `Float::nan()` if the + /// number is `Float::nan()`. + fn abs(self) -> Self; + /// Returns a number that represents the sign of `self`. + /// + /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` + /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` + /// - `Float::nan()` if the number is `Float::nan()` + fn signum(self) -> Self; + /// Returns `true` if `self` is positive, including `+0.0` and + /// `Float::infinity()`. + fn is_positive(self) -> bool; + /// Returns `true` if `self` is negative, including `-0.0` and + /// `Float::neg_infinity()`. + fn is_negative(self) -> bool; + /// Fused multiply-add. Computes `(self * a) + b` with only one rounding /// error. This produces a more accurate result with better performance than /// a separate multiplication operation followed by an add. @@ -1494,14 +1460,6 @@ one_impl!(i64, 1i64) one_impl!(f32, 1.0f32) one_impl!(f64, 1.0f64) -#[deprecated = "Use `Signed::abs`"] -pub fn abs(value: T) -> T { - value.abs() -} -#[deprecated = "Use `Signed::signum`"] -pub fn signum(value: T) -> T { - value.signum() -} #[deprecated = "Use `UnsignedInt::next_power_of_two`"] pub fn next_power_of_two(n: T) -> T { n.next_power_of_two() -- cgit 1.4.1-3-g733a5