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