pub struct Fixed<const P: usize, const S: usize>(/* private fields */);Expand description
Fixed-point decimal with fixed precision and scale.
Fixed<P, S>, where P in 1..=38 is the “precision” and S in 0..=P
is the “scale”, represents a signed decimal number in which S - P digits
precede the decimal point and S digits follow it. The table below shows
the maximum values for a few combinations of P and S. For each type,
the minimum value is the negation of the maximum:
| Type | Maximum Value |
|---|---|
Fixed<5,5> | 0.99999 |
Fixed<5,4> | 9.9999 |
Fixed<5,3> | 99.999 |
Fixed<5,2> | 999.99 |
Fixed<5,1> | 9,999.9 |
Fixed<5,0> | 99,999 |
Fixed<38,0> | 99,999,999,999,999,999,999,999,999,999,999,999,999 |
Fixed<38,5> | 999,999,999,999,999,999,999,999,999,999,999.99999 |
§Implementation
Fixed<P, S> internally contains a single i128 that represents a value
x as x * 10**P. This limits S to 38 because 10**38 ≤ 2**127 - 1 < 10**39. A single i64 would be sufficient for S ≤ 18, and a single
i32 for S ≤ 9, but the implementation does not optimize for those cases.
Implementations§
Source§impl<const P: usize, const S: usize> Fixed<P, S>
impl<const P: usize, const S: usize> Fixed<P, S>
Sourcepub const MIN: Self
pub const MIN: Self
Smallest value for this type, e.g. -999.99 for Fixed<5,2>.
MIN is always -MAX.
Sourcepub const ONE: Self
pub const ONE: Self
1 in this type.
§Panic
If S == P, this is undefined because 1 is not a value in this type,
and referring to it yields a compile-time error.
Sourcepub const fn checked_div_integer(self, other: Self) -> Option<i128>
pub const fn checked_div_integer(self, other: Self) -> Option<i128>
Integer division, as defined for divide-integer in General Decimal
Arithmetic. Returns None if other is zero or the result is greater
than i128::MAX.
Sourcepub const fn strict_div_integer(self, other: Self) -> i128
pub const fn strict_div_integer(self, other: Self) -> i128
Integer division like checked_div_integer, but panic on error.
§Panic
Panics if other is zero or the result is greater than i128::MAX.
Sourcepub const fn checked_rem_integer(self, _other: Self) -> Option<Self>
pub const fn checked_rem_integer(self, _other: Self) -> Option<Self>
Remainder, as defined for remainder in General Decimal Arithmetic.
Returns None if other is zero.
Sourcepub const fn strict_rem_integer(self, other: Self) -> Self
pub const fn strict_rem_integer(self, other: Self) -> Self
Sourcepub const fn abs(self) -> Self
pub const fn abs(self) -> Self
Returns the absolute value. This is an exact calculation that cannot overflow.
Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if this value is negative, false if it is zero or positive.
Sourcepub fn checked_sqrt(self) -> Option<Self>
pub fn checked_sqrt(self) -> Option<Self>
Returns the square root of this value, rounded down, or None if this
value is negative.
It probably makes more sense to convert to f64 and take the
floating-point square root.
Sourcepub fn sqrt(self) -> Self
pub fn sqrt(self) -> Self
Returns the square root of this value, rounded down.
It probably makes more sense to convert to f64 and take the
floating-point square root.
§Panic
Panics if this value is negative.
Sourcepub fn checked_round(&self, n: i32) -> Option<Self>
pub fn checked_round(&self, n: i32) -> Option<Self>
Returns this value rounded to n digits after the decimal point, or
None if rounding caused overflow, n may be negative.
If the value is halfway between two integers, rounds away from zero.
Sourcepub fn round(&self, n: i32) -> Self
pub fn round(&self, n: i32) -> Self
Rounds to n digits after the decimal point, like checked_round.
If the value is halfway between two integers, rounds away from zero.
§Panic
Panics if rounding causes overflow.
Sourcepub fn checked_round_ties_even(&self, n: i32) -> Option<Self>
pub fn checked_round_ties_even(&self, n: i32) -> Option<Self>
Returns this value rounded to n digits after the decimal point, or
None if rounding caused overflow, n may be negative. If the value
is halfway between two integers, rounds toward an even least significant
digit.
Sourcepub fn round_ties_even(&self, n: i32) -> Self
pub fn round_ties_even(&self, n: i32) -> Self
Rounds to n digits after the decimal point, like
checked_round_ties_even. If the value is halfway between two
integers, rounds toward an even least significant digit.
§Panic
Panics if rounding causes overflow.
Sourcepub fn checked_floor(&self) -> Option<Self>
pub fn checked_floor(&self) -> Option<Self>
Returns this value rounded down to the nearest integer, or None if
rounding caused overflow.
Sourcepub fn truncate(&self) -> Self
pub fn truncate(&self) -> Self
Returns the integer part of this value, truncating non-integers toward zero. This is an exact calculation that cannot overflow.
Sourcepub fn checked_ceil(&self) -> Option<Self>
pub fn checked_ceil(&self) -> Option<Self>
Returns this value rounded up to the nearest integer, or None if
rounding caused overflow.
Sourcepub fn sign(&self) -> Fixed<1, 0>
pub fn sign(&self) -> Fixed<1, 0>
Returns -1 if this value is less than zero, 0 if this value is zero, and
1 if this value is greater than zero, as Fixed<1,0>.
Sourcepub fn checked_recip(&self) -> Option<Self>
pub fn checked_recip(&self) -> Option<Self>
Returns the reciprocal (inverse) of this value, 1/x, or None if x
is zero or 1/x is out of range.
Sourcepub fn recip(&self) -> Self
pub fn recip(&self) -> Self
Returns the reciprocal (inverse) of this value, 1/x. This works even
if 1 is out of range for this type, as long as 1/x is in range.
§Panic
Panics if x is zero or 1/x is out of range.
Sourcepub fn checked_powi(&self, exp: i32) -> Option<Self>
pub fn checked_powi(&self, exp: i32) -> Option<Self>
Returns this value raised to exp power, rounding toward zero, or
None if the result is out of range or if this value is 0 and exp is
nonpositive.
§Accuracy
For exp > 0, this computes intermediate results with more than S
digits of precision, if possible, to allow to better accuracy in the
result. For exp < 0, this isn’t implemented yet.
Sourcepub fn powi(&self, exp: i32) -> Self
pub fn powi(&self, exp: i32) -> Self
Returns this value raised to exp power, rounding toward zero.
For exp > 0, this computes intermediate results with more than S
digits of precision, if possible, to allow to better accuracy in the
result. For exp < 0, this isn’t implemented yet.
§Panics
Panics if the result is out of range or if this value is 0 and exp is
nonpositive.
Sourcepub fn next_up(&self) -> Option<Self>
pub fn next_up(&self) -> Option<Self>
Returns the least number greater than self, or None if this is
Self::MAX.
Sourcepub fn next_down(&self) -> Option<Self>
pub fn next_down(&self) -> Option<Self>
Returns the greatest number less than self, or None if this is
Self::MAX.
Sourcepub fn encode(&self) -> (i128, u8)
pub fn encode(&self) -> (i128, u8)
Encodes this value in a form that can be later deserialized into a new
Fixed with possibly a different precision and scale.
Sourcepub fn decode(value: i128, scale: u8) -> Option<Self>
pub fn decode(value: i128, scale: u8) -> Option<Self>
Returns a new value from the serialized form returned by encode. If
successful, returns the original value from before serialization. If S < scale, then some trailing decimals are lost, by rounding toward zero.
Returns None if the value is out of range for this type.
Source§impl<const P0: usize, const S0: usize> Fixed<P0, S0>
impl<const P0: usize, const S0: usize> Fixed<P0, S0>
Sourcepub fn convert<const P1: usize, const S1: usize>(&self) -> Option<Fixed<P1, S1>>
pub fn convert<const P1: usize, const S1: usize>(&self) -> Option<Fixed<P1, S1>>
Returns this value converted into another type Fixed<P1, S1>, or
None if this value is outside the range of the target type. If the
conversion is successful, then the result is exactly the same as the
original value if S1 >= S0, and rounded down otherwise.
This should be implemented as TryFrom but that conflicts with the
standard library
implementation.
Sourcepub fn convert_round_even<const P1: usize, const S1: usize>(
&self,
) -> Option<Fixed<P1, S1>>
pub fn convert_round_even<const P1: usize, const S1: usize>( &self, ) -> Option<Fixed<P1, S1>>
Returns this value converted into another type Fixed<P1, S1>, or
None if this value is outside the range of the target type. If the
conversion is successful, then the result is exactly the same as the
original value if S1 >= S0, and rounded to even otherwise.
Sourcepub fn checked_sign_generic<const P1: usize, const S1: usize>(
&self,
) -> Option<Fixed<P1, S1>>
pub fn checked_sign_generic<const P1: usize, const S1: usize>( &self, ) -> Option<Fixed<P1, S1>>
Returns -1 if this value is less than zero, 0 if this value is zero, and
1 if this value is greater than zero, in an arbitrary Fixed type.
Returns None on overflow (if this value is nonzero and S1 >= P1).
Sourcepub fn checked_add_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>(
self,
other: Fixed<P1, S1>,
) -> Option<Fixed<P2, S2>>
pub fn checked_add_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>( self, other: Fixed<P1, S1>, ) -> Option<Fixed<P2, S2>>
Calculates self + other, for operands with scale and precision (S0,P0) and
(S1,P1), respectively, producing a result with scale and precision
(S2,P2). The result is calculated exactly if possible and otherwise
rounded toward zero. Returns None if the result is not representable
in the result type.
Sourcepub fn checked_sub_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>(
self,
other: Fixed<P1, S1>,
) -> Option<Fixed<P2, S2>>
pub fn checked_sub_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>( self, other: Fixed<P1, S1>, ) -> Option<Fixed<P2, S2>>
Calculates self - other, for operands with scale and precision
(S0,P0) and (S1,P1), respectively, producing a result with scale and
precision (S2,P2). The result is calculated exactly if possible and
otherwise rounded toward zero. Returns None if the result is not
representable in the result type.
Sourcepub fn checked_mul_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>(
self,
other: Fixed<P1, S1>,
) -> Option<Fixed<P2, S2>>
pub fn checked_mul_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>( self, other: Fixed<P1, S1>, ) -> Option<Fixed<P2, S2>>
Calculates self * other, for operands with scale and precision
(S0,P0) and (S1,P1), respectively, producing a result with scale and
precision (S2,P2). The result is calculated exactly if possible and
otherwise rounded toward zero. Returns None if the result is not
representable in the result type.
Sourcepub fn checked_div_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>(
self,
other: Fixed<P1, S1>,
) -> Option<Fixed<P2, S2>>
pub fn checked_div_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>( self, other: Fixed<P1, S1>, ) -> Option<Fixed<P2, S2>>
Calculate self / other, for operands with scale and precision
(S0,P0) and (S1,P1), respectively, producing a result with scale and
precision (S2,P2). The result is calculated exactly if possible and
otherwise rounded toward zero. Returns None if the result is not
representable in the result type, or if other is zero.
Sourcepub fn strict_add_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>(
self,
other: Fixed<P1, S1>,
) -> Fixed<P2, S2>
pub fn strict_add_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>( self, other: Fixed<P1, S1>, ) -> Fixed<P2, S2>
Compute self + other, rounding toward zero, panicking if overflow
occurs.
Sourcepub fn strict_sub_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>(
self,
other: Fixed<P1, S1>,
) -> Fixed<P2, S2>
pub fn strict_sub_generic<const P1: usize, const S1: usize, const P2: usize, const S2: usize>( self, other: Fixed<P1, S1>, ) -> Fixed<P2, S2>
Compute self - other, rounding toward zero, panicking if overflow
occurs.
Source§impl<const P: usize, const S: usize> Fixed<P, S>
impl<const P: usize, const S: usize> Fixed<P, S>
Sourcepub const fn max_u128() -> u128
pub const fn max_u128() -> u128
Returns the maximum u128 that can be converted to this type.
The minimum is 0.
Sourcepub const fn max_isize() -> isize
pub const fn max_isize() -> isize
Returns the maximum isize that can be converted to this type.
Sourcepub const fn min_isize() -> isize
pub const fn min_isize() -> isize
Returns the minimum isize that can be converted to this type.
Sourcepub const fn max_usize() -> usize
pub const fn max_usize() -> usize
Returns the maximum usize that can be converted to this type.
The minimum is 0.
Sourcepub const fn max_u64() -> u64
pub const fn max_u64() -> u64
Returns the maximum u64 that can be converted to this type.
The minimum is 0.
Sourcepub const fn max_u32() -> u32
pub const fn max_u32() -> u32
Returns the maximum u32 that can be converted to this type.
The minimum is 0.
Trait Implementations§
Source§impl<const P: usize, const S: usize> AddAssign<&Fixed<P, S>> for Fixed<P, S>
impl<const P: usize, const S: usize> AddAssign<&Fixed<P, S>> for Fixed<P, S>
Source§fn add_assign(&mut self, other: &Fixed<P, S>)
fn add_assign(&mut self, other: &Fixed<P, S>)
Source§impl<const P: usize, const S: usize> AddAssign for Fixed<P, S>
impl<const P: usize, const S: usize> AddAssign for Fixed<P, S>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Source§impl<const P: usize, const S: usize> CheckedAdd for Fixed<P, S>
impl<const P: usize, const S: usize> CheckedAdd for Fixed<P, S>
Source§fn checked_add(&self, other: &Self) -> Option<Self>
fn checked_add(&self, other: &Self) -> Option<Self>
Returns the sum, which is exact, or None if the result is out of
range.
Source§impl<const P: usize, const S: usize> CheckedDiv for Fixed<P, S>
impl<const P: usize, const S: usize> CheckedDiv for Fixed<P, S>
Source§fn checked_div(&self, other: &Self) -> Option<Self>
fn checked_div(&self, other: &Self) -> Option<Self>
Returns the quotient, rounding toward zero, or None if other is zero
or the result is out of range.
Source§impl<const P: usize, const S: usize> CheckedMul for Fixed<P, S>
impl<const P: usize, const S: usize> CheckedMul for Fixed<P, S>
Source§fn checked_mul(&self, other: &Self) -> Option<Self>
fn checked_mul(&self, other: &Self) -> Option<Self>
Returns the product, rounding toward zero, or None if the result is
out of range.
Source§impl<const P: usize, const S: usize> CheckedSub for Fixed<P, S>
impl<const P: usize, const S: usize> CheckedSub for Fixed<P, S>
Source§fn checked_sub(&self, other: &Self) -> Option<Self>
fn checked_sub(&self, other: &Self) -> Option<Self>
Returns the difference, which is exact, or None if the result is out
of range.
Source§impl<const P: usize, const S: usize> DivAssign for Fixed<P, S>
impl<const P: usize, const S: usize> DivAssign for Fixed<P, S>
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
Divides self by other, rounding toward zero.
§Panic
Panics if other is zero or the result is out of range.
Source§impl<const P: usize, const S: usize> FromStr for Fixed<P, S>
impl<const P: usize, const S: usize> FromStr for Fixed<P, S>
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses s as Fixed.
This accepts the same forms as f64::from_str, except that it rejects
infinities and NaNs (which Fixed does not support), as well as
out-of-range values. Rounds overprecise values to the nearest
representable value, rounding halfway values to even.
Source§type Err = ParseFixedError
type Err = ParseFixedError
Source§impl<const P: usize, const S: usize> MulAssign for Fixed<P, S>
impl<const P: usize, const S: usize> MulAssign for Fixed<P, S>
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
Source§impl<const P: usize, const S: usize> Ord for Fixed<P, S>
impl<const P: usize, const S: usize> Ord for Fixed<P, S>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const P0: usize, const S0: usize, const P1: usize, const S1: usize> PartialEq<Fixed<P1, S1>> for Fixed<P0, S0>
impl<const P0: usize, const S0: usize, const P1: usize, const S1: usize> PartialEq<Fixed<P1, S1>> for Fixed<P0, S0>
Source§impl<const P0: usize, const S0: usize, const P1: usize, const S1: usize> PartialOrd<Fixed<P1, S1>> for Fixed<P0, S0>
impl<const P0: usize, const S0: usize, const P1: usize, const S1: usize> PartialOrd<Fixed<P1, S1>> for Fixed<P0, S0>
Source§impl<const P: usize, const S: usize> SubAssign for Fixed<P, S>
impl<const P: usize, const S: usize> SubAssign for Fixed<P, S>
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Subtracts other from self, which is an exact calculation.
§Panic
Panics if the result is out of range.