Rollup merge of #76941 - clarfonthey:is_subnormal, r=m-ou-se
Add f{32,64}::is_subnormal The docs recommend that you use dedicated methods instead of calling `classify` directly, although there isn't actually a way of checking if a number is subnormal without calling classify. There are dedicated methods for all other forms, excluding `is_zero` (which is just `== 0.0` anyway).
This commit is contained in:
commit
9b98f1d226
@ -441,6 +441,32 @@ impl f32 {
|
||||
self.abs_private() < Self::INFINITY
|
||||
}
|
||||
|
||||
/// Returns `true` if the number is [subnormal].
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(is_subnormal)]
|
||||
/// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
|
||||
/// let max = f32::MAX;
|
||||
/// let lower_than_min = 1.0e-40_f32;
|
||||
/// let zero = 0.0_f32;
|
||||
///
|
||||
/// assert!(!min.is_subnormal());
|
||||
/// assert!(!max.is_subnormal());
|
||||
///
|
||||
/// assert!(!zero.is_subnormal());
|
||||
/// assert!(!f32::NAN.is_subnormal());
|
||||
/// assert!(!f32::INFINITY.is_subnormal());
|
||||
/// // Values between `0` and `min` are Subnormal.
|
||||
/// assert!(lower_than_min.is_subnormal());
|
||||
/// ```
|
||||
/// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
|
||||
#[unstable(feature = "is_subnormal", issue = "79288")]
|
||||
#[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
|
||||
#[inline]
|
||||
pub const fn is_subnormal(self) -> bool {
|
||||
matches!(self.classify(), FpCategory::Subnormal)
|
||||
}
|
||||
|
||||
/// Returns `true` if the number is neither zero, infinite,
|
||||
/// [subnormal], or `NaN`.
|
||||
///
|
||||
|
@ -440,6 +440,32 @@ impl f64 {
|
||||
self.abs_private() < Self::INFINITY
|
||||
}
|
||||
|
||||
/// Returns `true` if the number is [subnormal].
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(is_subnormal)]
|
||||
/// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308_f64
|
||||
/// let max = f64::MAX;
|
||||
/// let lower_than_min = 1.0e-308_f64;
|
||||
/// let zero = 0.0_f64;
|
||||
///
|
||||
/// assert!(!min.is_subnormal());
|
||||
/// assert!(!max.is_subnormal());
|
||||
///
|
||||
/// assert!(!zero.is_subnormal());
|
||||
/// assert!(!f64::NAN.is_subnormal());
|
||||
/// assert!(!f64::INFINITY.is_subnormal());
|
||||
/// // Values between `0` and `min` are Subnormal.
|
||||
/// assert!(lower_than_min.is_subnormal());
|
||||
/// ```
|
||||
/// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
|
||||
#[unstable(feature = "is_subnormal", issue = "79288")]
|
||||
#[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
|
||||
#[inline]
|
||||
pub const fn is_subnormal(self) -> bool {
|
||||
matches!(self.classify(), FpCategory::Subnormal)
|
||||
}
|
||||
|
||||
/// Returns `true` if the number is neither zero, infinite,
|
||||
/// [subnormal], or `NaN`.
|
||||
///
|
||||
|
Loading…
x
Reference in New Issue
Block a user