| 1 | //! Returns a number that represents the sign of `self`. |
| 2 | |
| 3 | use super::F32; |
| 4 | |
| 5 | impl F32 { |
| 6 | /// Returns a number that represents the sign of `self`. |
| 7 | /// |
| 8 | /// * `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 9 | /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 10 | /// * `NAN` if the number is `NAN` |
| 11 | pub fn signum(self) -> Self { |
| 12 | if self.is_nan() { |
| 13 | Self::NAN |
| 14 | } else { |
| 15 | F32(1.0).copysign(self) |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | #[cfg (test)] |
| 21 | mod tests { |
| 22 | use super::F32; |
| 23 | |
| 24 | #[test ] |
| 25 | fn sanity_check() { |
| 26 | assert_eq!(F32::INFINITY.signum(), F32(1.0)); |
| 27 | assert_eq!(F32(0.0).signum(), F32(1.0)); |
| 28 | assert_eq!(F32(1.0).signum(), F32(1.0)); |
| 29 | assert_eq!(F32::NEG_INFINITY.signum(), F32(-1.0)); |
| 30 | assert_eq!(F32(-0.0).signum(), F32(-1.0)); |
| 31 | assert_eq!(F32(-1.0).signum(), F32(-1.0)); |
| 32 | } |
| 33 | } |
| 34 | |