Skip to content

Commit 9b98f1d

Browse files
authored
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).
2 parents c643dd2 + cf26f2f commit 9b98f1d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

library/core/src/num/f32.rs

+26
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,32 @@ impl f32 {
441441
self.abs_private() < Self::INFINITY
442442
}
443443

444+
/// Returns `true` if the number is [subnormal].
445+
///
446+
/// ```
447+
/// #![feature(is_subnormal)]
448+
/// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
449+
/// let max = f32::MAX;
450+
/// let lower_than_min = 1.0e-40_f32;
451+
/// let zero = 0.0_f32;
452+
///
453+
/// assert!(!min.is_subnormal());
454+
/// assert!(!max.is_subnormal());
455+
///
456+
/// assert!(!zero.is_subnormal());
457+
/// assert!(!f32::NAN.is_subnormal());
458+
/// assert!(!f32::INFINITY.is_subnormal());
459+
/// // Values between `0` and `min` are Subnormal.
460+
/// assert!(lower_than_min.is_subnormal());
461+
/// ```
462+
/// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
463+
#[unstable(feature = "is_subnormal", issue = "79288")]
464+
#[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
465+
#[inline]
466+
pub const fn is_subnormal(self) -> bool {
467+
matches!(self.classify(), FpCategory::Subnormal)
468+
}
469+
444470
/// Returns `true` if the number is neither zero, infinite,
445471
/// [subnormal], or `NaN`.
446472
///

library/core/src/num/f64.rs

+26
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,32 @@ impl f64 {
440440
self.abs_private() < Self::INFINITY
441441
}
442442

443+
/// Returns `true` if the number is [subnormal].
444+
///
445+
/// ```
446+
/// #![feature(is_subnormal)]
447+
/// let min = f64::MIN_POSITIVE; // 2.2250738585072014e-308_f64
448+
/// let max = f64::MAX;
449+
/// let lower_than_min = 1.0e-308_f64;
450+
/// let zero = 0.0_f64;
451+
///
452+
/// assert!(!min.is_subnormal());
453+
/// assert!(!max.is_subnormal());
454+
///
455+
/// assert!(!zero.is_subnormal());
456+
/// assert!(!f64::NAN.is_subnormal());
457+
/// assert!(!f64::INFINITY.is_subnormal());
458+
/// // Values between `0` and `min` are Subnormal.
459+
/// assert!(lower_than_min.is_subnormal());
460+
/// ```
461+
/// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
462+
#[unstable(feature = "is_subnormal", issue = "79288")]
463+
#[rustc_const_unstable(feature = "const_float_classify", issue = "72505")]
464+
#[inline]
465+
pub const fn is_subnormal(self) -> bool {
466+
matches!(self.classify(), FpCategory::Subnormal)
467+
}
468+
443469
/// Returns `true` if the number is neither zero, infinite,
444470
/// [subnormal], or `NaN`.
445471
///

0 commit comments

Comments
 (0)