typed_floats/types/f32/positive_finite.rs
1use crate::types::{f32, InvalidNumber, PositiveFinite};
2use const_fn::const_fn;
3
4impl PositiveFinite<f32> {
5 /// Creates a new value from a primitive type
6 /// It adds a little overhead compared to `new_unchecked`
7 /// because it checks that the value is valid
8 ///
9 /// # Examples
10 ///
11 /// ```
12 /// # use typed_floats::tf32::PositiveFinite;
13 /// let x = PositiveFinite::new(3.0).unwrap();
14 ///
15 /// assert_eq!(x, 3.0);
16 /// ```
17 ///
18 /// # Errors
19 /// Returns an error if the value is not valid
20 #[inline]
21 #[const_fn("1.83")]
22 pub const fn new(value: f32) -> Result<Self, InvalidNumber> {
23 if value.is_nan() {
24 return Err(InvalidNumber::NaN);
25 }
26
27 if value.is_infinite() {
28 return Err(InvalidNumber::Infinite);
29 }
30
31 if value.is_sign_negative() {
32 return Err(InvalidNumber::Negative);
33 }
34
35 Ok(Self(value))
36 }
37
38 /// Creates a new value from a primitive type with zero overhead (in release mode).
39 /// It is up to the caller to ensure that the value is valid
40 ///
41 /// # Examples
42 ///
43 /// ```
44 /// # use typed_floats::tf32::PositiveFinite;
45 /// let x = unsafe { PositiveFinite::new_unchecked(3.0) };
46 ///
47 /// assert_eq!(x, 3.0);
48 /// ```
49 /// # Safety
50 /// The caller must ensure that the value is valid.
51 /// It will panic in debug mode if the value is not valid,
52 /// but in release mode the behavior is undefined
53 #[inline]
54 #[must_use]
55 #[const_fn("1.83")]
56 pub const unsafe fn new_unchecked(value: f32) -> Self {
57 crate::macros::new_unchecked!(value, PositiveFinite)
58 }
59
60 /// Returns the value as a primitive type
61 ///
62 /// # Examples
63 ///
64 /// ```
65 /// use typed_floats::tf32::PositiveFinite;
66 ///
67 /// let x = PositiveFinite::new(3.0).unwrap();
68 ///
69 /// let y: f32 = x.into();
70 ///
71 /// assert_eq!(y, 3.0);
72 /// ```
73 #[inline]
74 #[must_use]
75 pub const fn get(&self) -> f32 {
76 self.0
77 }
78
79 /// Returns `true` if this value is NaN.
80 /// This is never the case for the provided types
81 ///
82 /// # Examples
83 ///
84 /// ```
85 /// use typed_floats::tf32::PositiveFinite;
86 /// let x: PositiveFinite = 3.0.try_into().unwrap();
87 ///
88 /// assert_eq!(x.is_nan(), false);
89 /// ```
90 ///
91 /// See [`f32::is_nan()`] for more details.
92 #[inline]
93 #[must_use]
94 #[const_fn("1.83")]
95 pub const fn is_nan(&self) -> bool {
96 false
97 }
98
99 /// Returns `true` if this value is positive infinity or negative infinity.
100 ///
101 /// # Examples
102 ///
103 /// ```
104 /// use typed_floats::tf32::PositiveFinite;
105 /// let x: PositiveFinite = 3.0.try_into().unwrap();
106 ///
107 /// assert_eq!(x.is_infinite(), false);
108 /// ```
109 ///
110 /// See [`f32::is_infinite()`] for more details.
111 #[inline]
112 #[must_use]
113 #[const_fn("1.83")]
114 pub const fn is_infinite(&self) -> bool {
115 false
116 }
117
118 /// Returns `true` if this number is positive infinity nor negative infinity.
119 ///
120 /// # Examples
121 ///
122 /// ```
123 /// use typed_floats::tf32::PositiveFinite;
124 /// let x: PositiveFinite = 3.0.try_into().unwrap();
125 ///
126 /// assert_eq!(x.is_finite(), true);
127 /// ```
128 ///
129 /// See [`f32::is_finite()`] for more details.
130 #[inline]
131 #[must_use]
132 #[const_fn("1.83")]
133 pub const fn is_finite(&self) -> bool {
134 true
135 }
136
137 /// Returns `true` if the number is [subnormal](https://en.wikipedia.org/wiki/Denormal_number).
138 ///
139 /// # Examples
140 ///
141 /// ```
142 /// use typed_floats::tf32::PositiveFinite;
143 /// let x: PositiveFinite = 3.0.try_into().unwrap();
144 ///
145 /// assert_eq!(x.is_subnormal(), false);
146 /// ```
147 ///
148 /// See [`f32::is_subnormal()`] for more details.
149 #[inline]
150 #[must_use]
151 #[const_fn("1.83")]
152 pub const fn is_subnormal(&self) -> bool {
153 self.0.is_subnormal()
154 }
155
156 /// Returns `true` if the number is neither zero, infinite or [subnormal](https://en.wikipedia.org/wiki/Denormal_number).
157 ///
158 /// # Examples
159 ///
160 /// ```
161 /// use typed_floats::tf32::PositiveFinite;
162 /// let x: PositiveFinite = 3.0.try_into().unwrap();
163 ///
164 /// assert_eq!(x.is_normal(), true);
165 /// ```
166 ///
167 /// See [`f32::is_normal()`] for more details.
168 #[inline]
169 #[must_use]
170 #[const_fn("1.83")]
171 pub const fn is_normal(&self) -> bool {
172 self.0.is_normal()
173 }
174
175 /// Returns the floating point category of the number. If only one property
176 /// is going to be tested, it is generally faster to use the specific
177 /// predicate instead.
178 ///
179 /// # Examples
180 ///
181 /// ```
182 /// use typed_floats::tf32::PositiveFinite;
183 /// let x: PositiveFinite = 3.0.try_into().unwrap();
184 ///
185 /// assert_eq!(x.classify(), core::num::FpCategory::Normal);
186 /// ```
187 ///
188 /// See [`f32::classify()`] for more details.
189 #[inline]
190 #[must_use]
191 #[const_fn("1.83")]
192 pub const fn classify(&self) -> core::num::FpCategory {
193 self.0.classify()
194 }
195
196 /// Returns `true` if `self` has a positive sign, including `+0.0` and positive infinity.
197 ///
198 /// # Examples
199 ///
200 /// ```
201 /// use typed_floats::tf32::PositiveFinite;
202 /// let x: PositiveFinite = 3.0.try_into().unwrap();
203 ///
204 /// assert_eq!(x.is_sign_positive(), true);
205 /// ```
206 ///
207 /// See [`f32::is_sign_positive()`] for more details.
208 #[inline]
209 #[must_use]
210 #[const_fn("1.83")]
211 pub const fn is_sign_positive(&self) -> bool {
212 true
213 }
214
215 /// Returns `true` if `self` has a negative sign, including `-0.0` and negative infinity.
216 ///
217 /// # Examples
218 ///
219 /// ```
220 /// use typed_floats::tf32::PositiveFinite;
221 /// let x: PositiveFinite = 3.0.try_into().unwrap();
222 ///
223 /// assert_eq!(x.is_sign_negative(), false);
224 /// ```
225 ///
226 /// See [`f32::is_sign_negative()`] for more details.
227 #[inline]
228 #[must_use]
229 #[const_fn("1.83")]
230 pub const fn is_sign_negative(&self) -> bool {
231 false
232 }
233
234 /// Returns `true` if the number is negative zero.
235 ///
236 /// # Examples
237 ///
238 /// ```
239 /// use typed_floats::tf32::PositiveFinite;
240 /// let x: PositiveFinite = 3.0.try_into().unwrap();
241 /// let y: PositiveFinite = 0.0.try_into().unwrap();
242 ///
243 /// assert_eq!(x.is_negative_zero(), false);
244 /// assert_eq!(y.is_negative_zero(), false);
245 /// ```
246 #[inline]
247 #[must_use]
248 #[const_fn("1.83")]
249 pub const fn is_negative_zero(&self) -> bool {
250 false
251 }
252
253 /// Returns `true` if the number is positive zero.
254 ///
255 /// # Examples
256 ///
257 /// ```
258 /// use typed_floats::tf32::PositiveFinite;
259 /// let x: PositiveFinite = 3.0.try_into().unwrap();
260 /// let y: PositiveFinite = 0.0.try_into().unwrap();
261 ///
262 /// assert_eq!(x.is_positive_zero(), false);
263 /// assert_eq!(y.is_positive_zero(), true);
264 /// ```
265 #[inline]
266 #[must_use]
267 #[const_fn("1.83")]
268 pub const fn is_positive_zero(&self) -> bool {
269 self.0 == 0.0
270 }
271}