pub trait Decimal:
Copy
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Default
+ Debug
+ Display
+ Hash
+ Add<Output = Self>
+ Sub<Output = Self>
+ Mul<Output = Self>
+ Div<Output = Self>
+ Rem<Output = Self>
+ Neg<Output = Self>
+ AddAssign
+ SubAssign
+ MulAssign
+ DivAssign
+ RemAssign
+ BitAnd<Output = Self>
+ BitOr<Output = Self>
+ BitXor<Output = Self>
+ Not<Output = Self>
+ Shl<u32, Output = Self>
+ Shr<u32, Output = Self> {
type Storage: Copy + PartialEq + Eq;
const SCALE: u32;
const MAX_SCALE: u32;
const ZERO: Self;
const ONE: Self;
const MAX: Self;
const MIN: Self;
Show 59 methods
// Required methods
fn multiplier() -> Self::Storage;
fn from_bits(raw: Self::Storage) -> Self;
fn to_bits(self) -> Self::Storage;
fn scale(self) -> u32;
fn abs(self) -> Self;
fn signum(self) -> Self;
fn is_positive(self) -> bool;
fn is_negative(self) -> bool;
fn is_nan(self) -> bool;
fn is_infinite(self) -> bool;
fn is_finite(self) -> bool;
fn div_euclid(self, rhs: Self) -> Self;
fn rem_euclid(self, rhs: Self) -> Self;
fn div_floor(self, rhs: Self) -> Self;
fn div_ceil(self, rhs: Self) -> Self;
fn abs_diff(self, rhs: Self) -> Self;
fn midpoint(self, rhs: Self) -> Self;
fn mul_add(self, a: Self, b: Self) -> Self;
fn pow(self, exp: u32) -> Self;
fn powi(self, exp: i32) -> Self;
fn checked_pow(self, exp: u32) -> Option<Self>;
fn wrapping_pow(self, exp: u32) -> Self;
fn saturating_pow(self, exp: u32) -> Self;
fn overflowing_pow(self, exp: u32) -> (Self, bool);
fn checked_add(self, rhs: Self) -> Option<Self>;
fn checked_sub(self, rhs: Self) -> Option<Self>;
fn checked_mul(self, rhs: Self) -> Option<Self>;
fn checked_div(self, rhs: Self) -> Option<Self>;
fn checked_neg(self) -> Option<Self>;
fn checked_rem(self, rhs: Self) -> Option<Self>;
fn wrapping_add(self, rhs: Self) -> Self;
fn wrapping_sub(self, rhs: Self) -> Self;
fn wrapping_mul(self, rhs: Self) -> Self;
fn wrapping_div(self, rhs: Self) -> Self;
fn wrapping_neg(self) -> Self;
fn wrapping_rem(self, rhs: Self) -> Self;
fn saturating_add(self, rhs: Self) -> Self;
fn saturating_sub(self, rhs: Self) -> Self;
fn saturating_mul(self, rhs: Self) -> Self;
fn saturating_div(self, rhs: Self) -> Self;
fn saturating_neg(self) -> Self;
fn overflowing_add(self, rhs: Self) -> (Self, bool);
fn overflowing_sub(self, rhs: Self) -> (Self, bool);
fn overflowing_mul(self, rhs: Self) -> (Self, bool);
fn overflowing_div(self, rhs: Self) -> (Self, bool);
fn overflowing_neg(self) -> (Self, bool);
fn overflowing_rem(self, rhs: Self) -> (Self, bool);
fn from_i32(value: i32) -> Self;
fn to_int(self) -> i64;
fn to_int_with(self, mode: RoundingMode) -> i64;
fn from_f64(value: f64) -> Self;
fn from_f64_with(value: f64, mode: RoundingMode) -> Self;
fn to_f64(self) -> f64;
fn to_f32(self) -> f32;
// Provided methods
fn is_zero(self) -> bool { ... }
fn is_one(self) -> bool { ... }
fn is_normal(self) -> bool { ... }
fn sum<I>(iter: I) -> Self
where I: IntoIterator<Item = Self> { ... }
fn product<I>(iter: I) -> Self
where I: IntoIterator<Item = Self> { ... }
}Expand description
Scaled fixed-point decimal type with a compile-time SCALE and a
fixed-width integer Storage.
See the module-level documentation for the full surface and what’s intentionally not on the trait.
§Precision
N/A: this is a trait definition; no arithmetic is performed.
Required Associated Constants§
Sourceconst SCALE: u32
const SCALE: u32
The decimal scale of this type, equal to the const-generic
parameter. One LSB of storage represents 10^-SCALE.
Required Associated Types§
Required Methods§
Sourcefn multiplier() -> Self::Storage
fn multiplier() -> Self::Storage
Returns 10^SCALE, the factor that converts a logical integer
to its storage representation.
Sourcefn scale(self) -> u32
fn scale(self) -> u32
Returns the decimal scale of this value (equal to
Self::SCALE; provided for ergonomic method-call syntax).
Sourcefn abs(self) -> Self
fn abs(self) -> Self
Absolute value. Self::MIN.abs() panics in debug, wraps to
Self::MIN in release (mirroring the storage type).
Sourcefn is_positive(self) -> bool
fn is_positive(self) -> bool
true when self > ZERO.
Sourcefn is_negative(self) -> bool
fn is_negative(self) -> bool
true when self < ZERO.
Sourcefn is_infinite(self) -> bool
fn is_infinite(self) -> bool
Always false — fixed-point decimals cannot represent infinity.
Sourcefn div_euclid(self, rhs: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
Euclidean division: integer quotient (rounded so the remainder
is non-negative) scaled back into Self. Panics on rhs == ZERO.
Sourcefn rem_euclid(self, rhs: Self) -> Self
fn rem_euclid(self, rhs: Self) -> Self
Euclidean remainder, non-negative when rhs != ZERO. Panics on
rhs == ZERO.
Sourcefn div_floor(self, rhs: Self) -> Self
fn div_floor(self, rhs: Self) -> Self
Floor-rounded division (toward -∞). Panics on rhs == ZERO.
Sourcefn midpoint(self, rhs: Self) -> Self
fn midpoint(self, rhs: Self) -> Self
Midpoint of self and rhs (rounding toward -∞), computed
without intermediate overflow.
Sourcefn mul_add(self, a: Self, b: Self) -> Self
fn mul_add(self, a: Self, b: Self) -> Self
self * a + b — mirrors the f64::mul_add call shape so
f64-generic numeric code can monomorphise to a decimal type.
Sourcefn pow(self, exp: u32) -> Self
fn pow(self, exp: u32) -> Self
self^exp via square-and-multiply. Overflow follows Mul
(debug panic, release wrap).
Sourcefn powi(self, exp: i32) -> Self
fn powi(self, exp: i32) -> Self
Signed integer exponent. Negative exp produces
ONE / self.pow(|exp|); the divide truncates at the type’s
scale.
Sourcefn checked_pow(self, exp: u32) -> Option<Self>
fn checked_pow(self, exp: u32) -> Option<Self>
Some(self^exp), or None if any intermediate step overflows.
Sourcefn wrapping_pow(self, exp: u32) -> Self
fn wrapping_pow(self, exp: u32) -> Self
Wrapping pow. Each multiplication step wraps in the storage
type.
Sourcefn saturating_pow(self, exp: u32) -> Self
fn saturating_pow(self, exp: u32) -> Self
Saturating pow — clamps to MAX / MIN on overflow, with
the sign matching the mathematical result.
Sourcefn overflowing_pow(self, exp: u32) -> (Self, bool)
fn overflowing_pow(self, exp: u32) -> (Self, bool)
(self^exp, overflowed) — the wrapping result paired with a
boolean.
Sourcefn checked_add(self, rhs: Self) -> Option<Self>
fn checked_add(self, rhs: Self) -> Option<Self>
Some(self + rhs), or None if the sum overflows.
Sourcefn checked_sub(self, rhs: Self) -> Option<Self>
fn checked_sub(self, rhs: Self) -> Option<Self>
Some(self - rhs), or None if the difference overflows.
Sourcefn checked_mul(self, rhs: Self) -> Option<Self>
fn checked_mul(self, rhs: Self) -> Option<Self>
Some(self * rhs), or None if the scaled product overflows.
Sourcefn checked_div(self, rhs: Self) -> Option<Self>
fn checked_div(self, rhs: Self) -> Option<Self>
Some(self / rhs), or None if rhs == ZERO or the quotient
overflows the storage.
Sourcefn checked_neg(self) -> Option<Self>
fn checked_neg(self) -> Option<Self>
Some(-self), or None when self == MIN.
Sourcefn checked_rem(self, rhs: Self) -> Option<Self>
fn checked_rem(self, rhs: Self) -> Option<Self>
Some(self % rhs), or None on divide-by-zero / MIN % -ONE.
Sourcefn wrapping_add(self, rhs: Self) -> Self
fn wrapping_add(self, rhs: Self) -> Self
Two’s-complement wrapping +.
Sourcefn wrapping_sub(self, rhs: Self) -> Self
fn wrapping_sub(self, rhs: Self) -> Self
Two’s-complement wrapping -.
Sourcefn wrapping_mul(self, rhs: Self) -> Self
fn wrapping_mul(self, rhs: Self) -> Self
Wrapping * — intermediate widens for overflow detection, the
final narrowing wraps.
Sourcefn wrapping_div(self, rhs: Self) -> Self
fn wrapping_div(self, rhs: Self) -> Self
Wrapping / — panics on rhs == ZERO, matching
i128::wrapping_div.
Sourcefn wrapping_neg(self) -> Self
fn wrapping_neg(self) -> Self
Wrapping -self; MIN.wrapping_neg() == MIN.
Sourcefn wrapping_rem(self, rhs: Self) -> Self
fn wrapping_rem(self, rhs: Self) -> Self
Wrapping % — panics on rhs == ZERO.
Sourcefn saturating_add(self, rhs: Self) -> Self
fn saturating_add(self, rhs: Self) -> Self
Saturating +.
Sourcefn saturating_sub(self, rhs: Self) -> Self
fn saturating_sub(self, rhs: Self) -> Self
Saturating -.
Sourcefn saturating_mul(self, rhs: Self) -> Self
fn saturating_mul(self, rhs: Self) -> Self
Saturating * — sign of the saturated bound matches the
mathematical product.
Sourcefn saturating_div(self, rhs: Self) -> Self
fn saturating_div(self, rhs: Self) -> Self
Saturating / — divide-by-zero saturates to MAX / MIN
according to the sign of self.
Sourcefn saturating_neg(self) -> Self
fn saturating_neg(self) -> Self
Saturating -self — MIN.saturating_neg() == MAX.
Sourcefn overflowing_add(self, rhs: Self) -> (Self, bool)
fn overflowing_add(self, rhs: Self) -> (Self, bool)
Overflowing +.
Sourcefn overflowing_sub(self, rhs: Self) -> (Self, bool)
fn overflowing_sub(self, rhs: Self) -> (Self, bool)
Overflowing -.
Sourcefn overflowing_mul(self, rhs: Self) -> (Self, bool)
fn overflowing_mul(self, rhs: Self) -> (Self, bool)
Overflowing *.
Sourcefn overflowing_div(self, rhs: Self) -> (Self, bool)
fn overflowing_div(self, rhs: Self) -> (Self, bool)
Overflowing / — overflowed is true on out-of-range or
divide-by-zero.
Sourcefn overflowing_neg(self) -> (Self, bool)
fn overflowing_neg(self) -> (Self, bool)
Overflowing -self — overflowed is true iff self == MIN.
Sourcefn overflowing_rem(self, rhs: Self) -> (Self, bool)
fn overflowing_rem(self, rhs: Self) -> (Self, bool)
Overflowing %.
Sourcefn from_i32(value: i32) -> Self
fn from_i32(value: i32) -> Self
Construct from an i32, scaling by 10^SCALE. Width-generic
integer constructor; for wider source integers use the
concrete type’s from_int (whose IntSrc parameter varies
per width).
Sourcefn to_int(self) -> i64
fn to_int(self) -> i64
Convert to i64 using the crate-default rounding mode, with
saturating overflow on out-of-range integer parts.
Sourcefn to_int_with(self, mode: RoundingMode) -> i64
fn to_int_with(self, mode: RoundingMode) -> i64
Convert to i64 using the supplied rounding mode for the
fractional discard step. Saturating overflow on out-of-range
integer parts.
Sourcefn from_f64(value: f64) -> Self
fn from_f64(value: f64) -> Self
Construct from f64 using the crate-default rounding mode.
NaN saturates to ZERO; ±∞ saturates to MAX / MIN.
Sourcefn from_f64_with(value: f64, mode: RoundingMode) -> Self
fn from_f64_with(value: f64, mode: RoundingMode) -> Self
Construct from f64 using the supplied rounding mode.
Provided Methods§
Sourcefn is_normal(self) -> bool
fn is_normal(self) -> bool
true for every non-zero value (a fixed-point decimal has no
subnormals).
Sourcefn sum<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = Self>,
fn sum<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = Self>,
Sums an iterator of decimals of this width, starting from
ZERO. Width-generic convenience for iter.fold(ZERO, +).
Sourcefn product<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = Self>,
fn product<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = Self>,
Multiplies an iterator of decimals of this width, starting from
ONE. Width-generic convenience for iter.fold(ONE, *).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.