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