pub struct PositiveSign<T>(/* private fields */);Expand description
A floating-point number which is not NaN and whose sign bit is positive.
The allowed values consist of positive zero, positive infinity, and every value in between those. This set of values means that this type:
- Implements
Eqstraightforwardly; neither NaN nor signed zeroes can cause problems with using it as a map key for caching, interning, etc. - Is closed under multiplication and addition, so unlike
NotNan, cannot cause a panic from uncautious arithmetic.
The permitted values of a PositiveSign<T> are a subset of those of a NotNan<T>.
(This may not be guaranteed if T implements numeric traits in inconsistent ways,
but may be assumed for f32 and f64.)
The arithmetic behavior of PositiveSign<T> is not identical to T.
Specifically, the value of 0. * fXX::INFINITY is NaN, but the value of
PositiveSign(0.) * PositiveSign(fXX::INFINITY) is PositiveSign(0.) instead.
Our assumption here is that for the applications where PositiveSign is suitable, infinities
are either impossible or arise as “would be finite but it is too large to be represented”,
and do not arise as the reciprocal of zero, and thus we can treat “zero times anything
is zero” as being a more important property than “infinity times anything is infinity”.
Implementations§
Source§impl<T: FloatCore> PositiveSign<T>
impl<T: FloatCore> PositiveSign<T>
Sourcepub const unsafe fn new_unchecked(value: T) -> Self
pub const unsafe fn new_unchecked(value: T) -> Self
Construct PositiveSign without checking the value.
§Safety
value must not be NaN and must have a positive sign bit.
Note that value >= 0. is not a sufficient condition,
because it does not exclude negative zero.
Sourcepub const fn into_inner(self) -> T
pub const fn into_inner(self) -> T
Unwraps the value without modifying it.
Sourcepub fn round(self) -> Self
pub fn round(self) -> Self
Returns the nearest integer to self.
If a value is half-way between two integers, round up from 0.0.
Source§impl PositiveSign<f32>
impl PositiveSign<f32>
Sourcepub const fn new_strict(value: f32) -> Self
pub const fn new_strict(value: f32) -> Self
Wraps the given value in PositiveSign.
- If
valueis positive (including positive infinity), returns wrappedvalue. - If
valueis zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0. - If
valueis negative non-zero or NaN, panics.
Sourcepub const fn new_clamped(value: f32) -> Self
pub const fn new_clamped(value: f32) -> Self
Wraps the given value in PositiveSign.
- If the value is NaN, panics.
- If the value has a negative sign, it is replaced with positive zero.
Sourcepub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
Subtract other from self; if the result would be negative, it is zero instead.
Source§impl PositiveSign<f64>
impl PositiveSign<f64>
Sourcepub const fn new_strict(value: f64) -> Self
pub const fn new_strict(value: f64) -> Self
Wraps the given value in PositiveSign.
- If
valueis positive (including positive infinity), returns wrappedvalue. - If
valueis zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0. - If
valueis negative non-zero or NaN, panics.
Sourcepub const fn new_clamped(value: f64) -> Self
pub const fn new_clamped(value: f64) -> Self
Wraps the given value in PositiveSign.
- If the value is NaN, panics.
- If the value has a negative sign, it is replaced with positive zero.
Sourcepub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
Subtract other from self; if the result would be negative, it is zero instead.
Trait Implementations§
Source§impl<T: FloatCore + Add<Output = T>> AddAssign for PositiveSign<T>
impl<T: FloatCore + Add<Output = T>> AddAssign for PositiveSign<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<T> AsRef<T> for PositiveSign<T>
impl<T> AsRef<T> for PositiveSign<T>
Source§impl<T: Clone> Clone for PositiveSign<T>
impl<T: Clone> Clone for PositiveSign<T>
Source§fn clone(&self) -> PositiveSign<T>
fn clone(&self) -> PositiveSign<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<T: Copy> Copy for PositiveSign<T>
Source§impl<T: Debug> Debug for PositiveSign<T>
impl<T: Debug> Debug for PositiveSign<T>
Source§impl<T: Display> Display for PositiveSign<T>
impl<T: Display> Display for PositiveSign<T>
impl<T: PartialEq> Eq for PositiveSign<T>
Source§impl From<PositiveSign<f32>> for NotNan<f32>
impl From<PositiveSign<f32>> for NotNan<f32>
Source§fn from(value: PositiveSign<f32>) -> Self
fn from(value: PositiveSign<f32>) -> Self
Source§impl From<PositiveSign<f32>> for f32
impl From<PositiveSign<f32>> for f32
Source§fn from(value: PositiveSign<f32>) -> Self
fn from(value: PositiveSign<f32>) -> Self
Source§impl From<PositiveSign<f64>> for NotNan<f64>
impl From<PositiveSign<f64>> for NotNan<f64>
Source§fn from(value: PositiveSign<f64>) -> Self
fn from(value: PositiveSign<f64>) -> Self
Source§impl From<PositiveSign<f64>> for f64
impl From<PositiveSign<f64>> for f64
Source§fn from(value: PositiveSign<f64>) -> Self
fn from(value: PositiveSign<f64>) -> Self
Source§impl<T: PrimitiveFloat> Hash for PositiveSign<T>
impl<T: PrimitiveFloat> Hash for PositiveSign<T>
Source§impl<T: FloatCore + Mul<Output = T>> Mul for PositiveSign<T>
impl<T: FloatCore + Mul<Output = T>> Mul for PositiveSign<T>
Source§fn mul(self, rhs: Self) -> Self::Output
fn mul(self, rhs: Self) -> Self::Output
This multiplication operation differs from standard floating-point multiplication in that multiplying zero by positive infinity returns zero instead of NaN. This is necessary for the type to be closed under multiplication.
Source§type Output = PositiveSign<T>
type Output = PositiveSign<T>
* operator.Source§impl<T: FloatCore> Mul<PositiveSign<T>> for ZeroOne<T>
impl<T: FloatCore> Mul<PositiveSign<T>> for ZeroOne<T>
Source§type Output = PositiveSign<T>
type Output = PositiveSign<T>
* operator.Source§impl<T: FloatCore + Mul<Output = T>> MulAssign for PositiveSign<T>
impl<T: FloatCore + Mul<Output = T>> MulAssign for PositiveSign<T>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<T: FloatCore + PartialOrd> Ord for PositiveSign<T>
impl<T: FloatCore + PartialOrd> Ord for PositiveSign<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for PositiveSign<T>
impl<T: PartialEq> PartialEq for PositiveSign<T>
Source§fn eq(&self, other: &PositiveSign<T>) -> bool
fn eq(&self, other: &PositiveSign<T>) -> bool
self and other values to be equal, and is used by ==.Source§impl<T: PartialEq> PartialEq<T> for PositiveSign<T>
impl<T: PartialEq> PartialEq<T> for PositiveSign<T>
Source§impl<T: FloatCore + PartialOrd> PartialOrd for PositiveSign<T>
impl<T: FloatCore + PartialOrd> PartialOrd for PositiveSign<T>
Source§impl<T: PartialOrd> PartialOrd<T> for PositiveSign<T>
impl<T: PartialOrd> PartialOrd<T> for PositiveSign<T>
impl<T> StructuralPartialEq for PositiveSign<T>
Source§impl<T> TryFrom<NotNan<T>> for PositiveSign<T>where
Self: TryFrom<T, Error = NotPositiveSign<T>>,
impl<T> TryFrom<NotNan<T>> for PositiveSign<T>where
Self: TryFrom<T, Error = NotPositiveSign<T>>,
Source§fn try_from(value: NotNan<T>) -> Result<Self, Self::Error>
fn try_from(value: NotNan<T>) -> Result<Self, Self::Error>
Checks that value is non-negative.
- If
valueis positive (including positive infinity), returns wrappedvalue. - If
valueis zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0. - If
valueis negative non-zero or NaN, returns an error.
Source§type Error = NotPositiveSign<T>
type Error = NotPositiveSign<T>
Source§impl TryFrom<f32> for PositiveSign<f32>
impl TryFrom<f32> for PositiveSign<f32>
Source§fn try_from(value: f32) -> Result<Self, Self::Error>
fn try_from(value: f32) -> Result<Self, Self::Error>
Checks that value is non-negative and non-NaN.
- If
valueis positive (including positive infinity), returns wrappedvalue. - If
valueis zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0. - If
valueis negative non-zero or NaN, returns an error.
Source§type Error = NotPositiveSign<f32>
type Error = NotPositiveSign<f32>
Source§impl TryFrom<f64> for PositiveSign<f64>
impl TryFrom<f64> for PositiveSign<f64>
Source§fn try_from(value: f64) -> Result<Self, Self::Error>
fn try_from(value: f64) -> Result<Self, Self::Error>
Checks that value is non-negative and non-NaN.
- If
valueis positive (including positive infinity), returns wrappedvalue. - If
valueis zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0. - If
valueis negative non-zero or NaN, returns an error.
Source§type Error = NotPositiveSign<f64>
type Error = NotPositiveSign<f64>
Auto Trait Implementations§
impl<T> Freeze for PositiveSign<T>where
T: Freeze,
impl<T> RefUnwindSafe for PositiveSign<T>where
T: RefUnwindSafe,
impl<T> Send for PositiveSign<T>where
T: Send,
impl<T> Sync for PositiveSign<T>where
T: Sync,
impl<T> Unpin for PositiveSign<T>where
T: Unpin,
impl<T> UnsafeUnpin for PositiveSign<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for PositiveSign<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.