rutil/number_traits/core_float_ops/
classification.rs

1use std::num::FpCategory;
2
3/// Classification operations.
4pub trait ClassificationOps {
5    /// Returns `true` if this value is `NaN`.
6    fn is_nan(self) -> bool;
7
8    /// Returns `true` if this value is positive infinity or negative infinity, and
9    /// `false` otherwise.
10    fn is_infinite(self) -> bool;
11
12    /// Returns `true` if this number is neither infinite nor `NaN`.
13    fn is_finite(self) -> bool;
14
15    /// Returns `true` if the number is [subnormal].
16    fn is_subnormal(self) -> bool;
17
18    /// Returns `true` if the number is neither zero, infinite,
19    /// [subnormal], or `NaN`.
20    fn is_normal(self) -> bool;
21
22    /// Returns the floating point category of the number. If only one property
23    /// is going to be tested, it is generally faster to use the specific
24    /// predicate instead.
25    fn classify(self) -> FpCategory;
26}
27
28/// Implements [`ClassificationOps`].
29macro_rules! impl_classification {
30    ($($t:ty),*) => {
31        $(impl ClassificationOps for $t {
32            #[inline] fn is_nan(self) -> bool { self.is_nan() }
33            #[inline] fn is_infinite(self) -> bool { self.is_infinite() }
34            #[inline] fn is_finite(self) -> bool { self.is_finite() }
35            #[inline] fn is_subnormal(self) -> bool { self.is_subnormal() }
36            #[inline] fn is_normal(self) -> bool { self.is_normal() }
37            #[inline] fn classify(self) -> FpCategory { self.classify() }
38        })*
39    };
40}
41
42impl_classification!(f32, f64);