numfmt/
numeric.rs

1/// An object is representable as a 64-bit floating point number.
2pub trait Numeric {
3    /// Perform the conversion.
4    fn to_f64(&self) -> f64;
5
6    /// Return if the number is NaN.
7    ///
8    /// The default implementation converts to `f64` and performs the test.
9    #[inline(always)]
10    fn is_nan(&self) -> bool {
11        self.to_f64().is_nan()
12    }
13
14    /// Return if the number is negative.
15    ///
16    /// The default implementation converts to `f64` and performs the test.
17    #[inline(always)]
18    fn is_negative(&self) -> bool {
19        self.to_f64().is_sign_negative()
20    }
21
22    /// Return if the number is infinite.
23    ///
24    /// The default implementation converts to `f64` and performs the test.
25    #[inline(always)]
26    fn is_infinite(&self) -> bool {
27        self.to_f64().is_infinite()
28    }
29
30    /// Return if the number is zero.
31    ///
32    /// The default implementation converts to `f64` and performs the test.
33    #[inline(always)]
34    fn is_zero(&self) -> bool {
35        self.to_f64() == 0.0
36    }
37}
38
39macro_rules! prim_impls {
40    (float => $($t:ty)*) => {
41        $(impl Numeric for $t {
42            #[inline(always)]
43            fn to_f64(&self) -> f64 {
44                (*self).into()
45            }
46
47            #[inline(always)]
48            fn is_nan(&self) -> bool {
49                Self::is_nan(*self)
50            }
51
52            #[inline(always)]
53            fn is_negative(&self) -> bool {
54                Self::is_sign_negative(*self)
55            }
56
57            #[inline(always)]
58            fn is_infinite(&self) -> bool {
59                Self::is_infinite(*self)
60            }
61
62            #[inline(always)]
63            fn is_zero(&self) -> bool {
64                *self == 0.0
65            }
66        })*
67    };
68    (uint => $($t:ty)*) => {
69        $(impl Numeric for $t {
70            #[inline(always)]
71            fn to_f64(&self) -> f64 {
72                *self as f64
73            }
74
75            #[inline(always)]
76            fn is_nan(&self) -> bool {
77                false
78            }
79
80            #[inline(always)]
81            fn is_negative(&self) -> bool {
82                false
83            }
84
85            #[inline(always)]
86            fn is_infinite(&self) -> bool {
87                false
88            }
89
90            #[inline(always)]
91            fn is_zero(&self) -> bool {
92                *self == 0
93            }
94        })*
95    };
96    (int => $($t:ty)*) => {
97        $(impl Numeric for $t {
98            #[inline(always)]
99            fn to_f64(&self) -> f64 {
100                *self as f64
101            }
102
103            #[inline(always)]
104            fn is_nan(&self) -> bool {
105                false
106            }
107
108            #[inline(always)]
109            fn is_negative(&self) -> bool {
110                *self < 0
111            }
112
113            #[inline(always)]
114            fn is_infinite(&self) -> bool {
115                false
116            }
117
118            #[inline(always)]
119            fn is_zero(&self) -> bool {
120                *self == 0
121            }
122        })*
123    };
124}
125
126prim_impls!(float => f32 f64);
127prim_impls!(uint => u8 u16 u32 u64 u128 usize);
128prim_impls!(int => i8 i16 i32 i64 i128 isize);