Fix NaN handling in is_sign_negative/positive

See #42425
This commit is contained in:
Simonas Kazlauskas
2017-06-04 21:44:57 +03:00
parent 0da9721ab4
commit 3b9fe77bfc
4 changed files with 40 additions and 46 deletions

View File

@@ -205,18 +205,25 @@ impl Float for f32 {
} }
} }
/// Returns `true` if `self` is positive, including `+0.0` and /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// `Float::infinity()`. /// positive sign bit and positive infinity.
#[inline] #[inline]
fn is_sign_positive(self) -> bool { fn is_sign_positive(self) -> bool {
self > 0.0 || (1.0 / self) == INFINITY !self.is_sign_negative()
} }
/// Returns `true` if `self` is negative, including `-0.0` and /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
/// `Float::neg_infinity()`. /// negative sign bit and negative infinity.
#[inline] #[inline]
fn is_sign_negative(self) -> bool { fn is_sign_negative(self) -> bool {
self < 0.0 || (1.0 / self) == NEG_INFINITY // IEEE754 says: isSignMinus(x) is true if and only if x has negative sign. isSignMinus
// applies to zeros and NaNs as well.
#[repr(C)]
union F32Bytes {
f: f32,
b: u32
}
unsafe { F32Bytes { f: self }.b & 0x8000_0000 != 0 }
} }
/// Returns the reciprocal (multiplicative inverse) of the number. /// Returns the reciprocal (multiplicative inverse) of the number.

View File

@@ -205,18 +205,23 @@ impl Float for f64 {
} }
} }
/// Returns `true` if `self` is positive, including `+0.0` and /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// `Float::infinity()`. /// positive sign bit and positive infinity.
#[inline] #[inline]
fn is_sign_positive(self) -> bool { fn is_sign_positive(self) -> bool {
self > 0.0 || (1.0 / self) == INFINITY !self.is_sign_negative()
} }
/// Returns `true` if `self` is negative, including `-0.0` and /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
/// `Float::neg_infinity()`. /// negative sign bit and negative infinity.
#[inline] #[inline]
fn is_sign_negative(self) -> bool { fn is_sign_negative(self) -> bool {
self < 0.0 || (1.0 / self) == NEG_INFINITY #[repr(C)]
union F64Bytes {
f: f64,
b: u64
}
unsafe { F64Bytes { f: self }.b & 0x8000_0000_0000_0000 != 0 }
} }
/// Returns the reciprocal (multiplicative inverse) of the number. /// Returns the reciprocal (multiplicative inverse) of the number.

View File

@@ -365,39 +365,29 @@ impl f32 {
#[inline] #[inline]
pub fn signum(self) -> f32 { num::Float::signum(self) } pub fn signum(self) -> f32 { num::Float::signum(self) }
/// Returns `true` if `self`'s sign bit is positive, including /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// `+0.0` and `INFINITY`. /// positive sign bit and positive infinity.
/// ///
/// ``` /// ```
/// use std::f32;
///
/// let nan = f32::NAN;
/// let f = 7.0_f32; /// let f = 7.0_f32;
/// let g = -7.0_f32; /// let g = -7.0_f32;
/// ///
/// assert!(f.is_sign_positive()); /// assert!(f.is_sign_positive());
/// assert!(!g.is_sign_positive()); /// assert!(!g.is_sign_positive());
/// // Requires both tests to determine if is `NaN`
/// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[inline] #[inline]
pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) } pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
/// Returns `true` if `self`'s sign is negative, including `-0.0` /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
/// and `NEG_INFINITY`. /// negative sign bit and negative infinity.
/// ///
/// ``` /// ```
/// use std::f32;
///
/// let nan = f32::NAN;
/// let f = 7.0f32; /// let f = 7.0f32;
/// let g = -7.0f32; /// let g = -7.0f32;
/// ///
/// assert!(!f.is_sign_negative()); /// assert!(!f.is_sign_negative());
/// assert!(g.is_sign_negative()); /// assert!(g.is_sign_negative());
/// // Requires both tests to determine if is `NaN`.
/// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[inline] #[inline]
@@ -1186,7 +1176,7 @@ mod tests {
assert!(!nan.is_infinite()); assert!(!nan.is_infinite());
assert!(!nan.is_finite()); assert!(!nan.is_finite());
assert!(!nan.is_normal()); assert!(!nan.is_normal());
assert!(!nan.is_sign_positive()); assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative()); assert!(!nan.is_sign_negative());
assert_eq!(Fp::Nan, nan.classify()); assert_eq!(Fp::Nan, nan.classify());
} }
@@ -1430,7 +1420,8 @@ mod tests {
assert!(!(-1f32).is_sign_positive()); assert!(!(-1f32).is_sign_positive());
assert!(!NEG_INFINITY.is_sign_positive()); assert!(!NEG_INFINITY.is_sign_positive());
assert!(!(1f32/NEG_INFINITY).is_sign_positive()); assert!(!(1f32/NEG_INFINITY).is_sign_positive());
assert!(!NAN.is_sign_positive()); assert!(NAN.is_sign_positive());
assert!(!(-NAN).is_sign_positive());
} }
#[test] #[test]
@@ -1443,6 +1434,7 @@ mod tests {
assert!(NEG_INFINITY.is_sign_negative()); assert!(NEG_INFINITY.is_sign_negative());
assert!((1f32/NEG_INFINITY).is_sign_negative()); assert!((1f32/NEG_INFINITY).is_sign_negative());
assert!(!NAN.is_sign_negative()); assert!(!NAN.is_sign_negative());
assert!((-NAN).is_sign_negative());
} }
#[test] #[test]

View File

@@ -303,21 +303,15 @@ impl f64 {
#[inline] #[inline]
pub fn signum(self) -> f64 { num::Float::signum(self) } pub fn signum(self) -> f64 { num::Float::signum(self) }
/// Returns `true` if `self`'s sign bit is positive, including /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
/// `+0.0` and `INFINITY`. /// positive sign bit and positive infinity.
/// ///
/// ``` /// ```
/// use std::f64;
///
/// let nan: f64 = f64::NAN;
///
/// let f = 7.0_f64; /// let f = 7.0_f64;
/// let g = -7.0_f64; /// let g = -7.0_f64;
/// ///
/// assert!(f.is_sign_positive()); /// assert!(f.is_sign_positive());
/// assert!(!g.is_sign_positive()); /// assert!(!g.is_sign_positive());
/// // Requires both tests to determine if is `NaN`
/// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[inline] #[inline]
@@ -328,21 +322,15 @@ impl f64 {
#[inline] #[inline]
pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) } pub fn is_positive(self) -> bool { num::Float::is_sign_positive(self) }
/// Returns `true` if `self`'s sign is negative, including `-0.0` /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
/// and `NEG_INFINITY`. /// negative sign bit and negative infinity.
/// ///
/// ``` /// ```
/// use std::f64;
///
/// let nan = f64::NAN;
///
/// let f = 7.0_f64; /// let f = 7.0_f64;
/// let g = -7.0_f64; /// let g = -7.0_f64;
/// ///
/// assert!(!f.is_sign_negative()); /// assert!(!f.is_sign_negative());
/// assert!(g.is_sign_negative()); /// assert!(g.is_sign_negative());
/// // Requires both tests to determine if is `NaN`.
/// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[inline] #[inline]
@@ -1103,7 +1091,7 @@ mod tests {
assert!(!nan.is_infinite()); assert!(!nan.is_infinite());
assert!(!nan.is_finite()); assert!(!nan.is_finite());
assert!(!nan.is_normal()); assert!(!nan.is_normal());
assert!(!nan.is_sign_positive()); assert!(nan.is_sign_positive());
assert!(!nan.is_sign_negative()); assert!(!nan.is_sign_negative());
assert_eq!(Fp::Nan, nan.classify()); assert_eq!(Fp::Nan, nan.classify());
} }
@@ -1346,7 +1334,8 @@ mod tests {
assert!(!(-1f64).is_sign_positive()); assert!(!(-1f64).is_sign_positive());
assert!(!NEG_INFINITY.is_sign_positive()); assert!(!NEG_INFINITY.is_sign_positive());
assert!(!(1f64/NEG_INFINITY).is_sign_positive()); assert!(!(1f64/NEG_INFINITY).is_sign_positive());
assert!(!NAN.is_sign_positive()); assert!(NAN.is_sign_positive());
assert!(!(-NAN).is_sign_positive());
} }
#[test] #[test]
@@ -1359,6 +1348,7 @@ mod tests {
assert!(NEG_INFINITY.is_sign_negative()); assert!(NEG_INFINITY.is_sign_negative());
assert!((1f64/NEG_INFINITY).is_sign_negative()); assert!((1f64/NEG_INFINITY).is_sign_negative());
assert!(!NAN.is_sign_negative()); assert!(!NAN.is_sign_negative());
assert!((-NAN).is_sign_negative());
} }
#[test] #[test]