Skip to main content

Decimal

Trait Decimal 

Source
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§

Source

const SCALE: u32

The decimal scale of this type, equal to the const-generic parameter. One LSB of storage represents 10^-SCALE.

Source

const MAX_SCALE: u32

The maximum legal SCALE for this width. Equal to the largest k such that 10^k fits in Self::Storage. For example, 38 for D38, 76 for D76.

Source

const ZERO: Self

The additive identity (logical value 0).

Source

const ONE: Self

The multiplicative identity (logical value 1).

Source

const MAX: Self

The largest representable value (storage equal to Self::Storage::MAX).

Source

const MIN: Self

The smallest representable value (storage equal to Self::Storage::MIN).

Required Associated Types§

Source

type Storage: Copy + PartialEq + Eq

Underlying integer storage type (e.g. i128 for D38<SCALE>).

Required Methods§

Source

fn multiplier() -> Self::Storage

Returns 10^SCALE, the factor that converts a logical integer to its storage representation.

Source

fn from_bits(raw: Self::Storage) -> Self

Constructs from a raw storage value.

Source

fn to_bits(self) -> Self::Storage

Returns the raw storage value.

Source

fn scale(self) -> u32

Returns the decimal scale of this value (equal to Self::SCALE; provided for ergonomic method-call syntax).

Source

fn abs(self) -> Self

Absolute value. Self::MIN.abs() panics in debug, wraps to Self::MIN in release (mirroring the storage type).

Source

fn signum(self) -> Self

+ONE, ZERO, or -ONE according to the sign of self.

Source

fn is_positive(self) -> bool

true when self > ZERO.

Source

fn is_negative(self) -> bool

true when self < ZERO.

Source

fn is_nan(self) -> bool

Always false — fixed-point decimals cannot represent NaN.

Source

fn is_infinite(self) -> bool

Always false — fixed-point decimals cannot represent infinity.

Source

fn is_finite(self) -> bool

Always true — every fixed-point decimal value is finite.

Source

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.

Source

fn rem_euclid(self, rhs: Self) -> Self

Euclidean remainder, non-negative when rhs != ZERO. Panics on rhs == ZERO.

Source

fn div_floor(self, rhs: Self) -> Self

Floor-rounded division (toward -∞). Panics on rhs == ZERO.

Source

fn div_ceil(self, rhs: Self) -> Self

Ceil-rounded division (toward +∞). Panics on rhs == ZERO.

Source

fn abs_diff(self, rhs: Self) -> Self

|self - rhs|, computed without intermediate overflow.

Source

fn midpoint(self, rhs: Self) -> Self

Midpoint of self and rhs (rounding toward -∞), computed without intermediate overflow.

Source

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.

Source

fn pow(self, exp: u32) -> Self

self^exp via square-and-multiply. Overflow follows Mul (debug panic, release wrap).

Source

fn powi(self, exp: i32) -> Self

Signed integer exponent. Negative exp produces ONE / self.pow(|exp|); the divide truncates at the type’s scale.

Source

fn checked_pow(self, exp: u32) -> Option<Self>

Some(self^exp), or None if any intermediate step overflows.

Source

fn wrapping_pow(self, exp: u32) -> Self

Wrapping pow. Each multiplication step wraps in the storage type.

Source

fn saturating_pow(self, exp: u32) -> Self

Saturating pow — clamps to MAX / MIN on overflow, with the sign matching the mathematical result.

Source

fn overflowing_pow(self, exp: u32) -> (Self, bool)

(self^exp, overflowed) — the wrapping result paired with a boolean.

Source

fn checked_add(self, rhs: Self) -> Option<Self>

Some(self + rhs), or None if the sum overflows.

Source

fn checked_sub(self, rhs: Self) -> Option<Self>

Some(self - rhs), or None if the difference overflows.

Source

fn checked_mul(self, rhs: Self) -> Option<Self>

Some(self * rhs), or None if the scaled product overflows.

Source

fn checked_div(self, rhs: Self) -> Option<Self>

Some(self / rhs), or None if rhs == ZERO or the quotient overflows the storage.

Source

fn checked_neg(self) -> Option<Self>

Some(-self), or None when self == MIN.

Source

fn checked_rem(self, rhs: Self) -> Option<Self>

Some(self % rhs), or None on divide-by-zero / MIN % -ONE.

Source

fn wrapping_add(self, rhs: Self) -> Self

Two’s-complement wrapping +.

Source

fn wrapping_sub(self, rhs: Self) -> Self

Two’s-complement wrapping -.

Source

fn wrapping_mul(self, rhs: Self) -> Self

Wrapping * — intermediate widens for overflow detection, the final narrowing wraps.

Source

fn wrapping_div(self, rhs: Self) -> Self

Wrapping /panics on rhs == ZERO, matching i128::wrapping_div.

Source

fn wrapping_neg(self) -> Self

Wrapping -self; MIN.wrapping_neg() == MIN.

Source

fn wrapping_rem(self, rhs: Self) -> Self

Wrapping %panics on rhs == ZERO.

Source

fn saturating_add(self, rhs: Self) -> Self

Saturating +.

Source

fn saturating_sub(self, rhs: Self) -> Self

Saturating -.

Source

fn saturating_mul(self, rhs: Self) -> Self

Saturating * — sign of the saturated bound matches the mathematical product.

Source

fn saturating_div(self, rhs: Self) -> Self

Saturating / — divide-by-zero saturates to MAX / MIN according to the sign of self.

Source

fn saturating_neg(self) -> Self

Saturating -selfMIN.saturating_neg() == MAX.

Source

fn overflowing_add(self, rhs: Self) -> (Self, bool)

Overflowing +.

Source

fn overflowing_sub(self, rhs: Self) -> (Self, bool)

Overflowing -.

Source

fn overflowing_mul(self, rhs: Self) -> (Self, bool)

Overflowing *.

Source

fn overflowing_div(self, rhs: Self) -> (Self, bool)

Overflowing /overflowed is true on out-of-range or divide-by-zero.

Source

fn overflowing_neg(self) -> (Self, bool)

Overflowing -selfoverflowed is true iff self == MIN.

Source

fn overflowing_rem(self, rhs: Self) -> (Self, bool)

Overflowing %.

Source

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).

Source

fn to_int(self) -> i64

Convert to i64 using the crate-default rounding mode, with saturating overflow on out-of-range integer parts.

Source

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.

Source

fn from_f64(value: f64) -> Self

Construct from f64 using the crate-default rounding mode. NaN saturates to ZERO; ±∞ saturates to MAX / MIN.

Source

fn from_f64_with(value: f64, mode: RoundingMode) -> Self

Construct from f64 using the supplied rounding mode.

Source

fn to_f64(self) -> f64

Convert to f64. Lossy when the storage magnitude exceeds f64’s ~15-digit exact range.

Source

fn to_f32(self) -> f32

Convert to f32. Lossy.

Provided Methods§

Source

fn is_zero(self) -> bool

true if this value is the additive identity.

Source

fn is_one(self) -> bool

true if this value is the multiplicative identity.

Source

fn is_normal(self) -> bool

true for every non-zero value (a fixed-point decimal has no subnormals).

Source

fn sum<I>(iter: I) -> Self
where I: IntoIterator<Item = Self>,

Sums an iterator of decimals of this width, starting from ZERO. Width-generic convenience for iter.fold(ZERO, +).

Source

fn product<I>(iter: I) -> Self
where 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.

Implementors§

Source§

impl<const SCALE: u32> Decimal for D9<SCALE>

Source§

const SCALE: u32 = SCALE

Source§

const MAX_SCALE: u32 = 9

Source§

const ZERO: Self = D9<SCALE>::ZERO

Source§

const ONE: Self = D9<SCALE>::ONE

Source§

const MAX: Self = D9<SCALE>::MAX

Source§

const MIN: Self = D9<SCALE>::MIN

Source§

type Storage = i32

Source§

impl<const SCALE: u32> Decimal for D18<SCALE>

Source§

const SCALE: u32 = SCALE

Source§

const MAX_SCALE: u32 = 18

Source§

const ZERO: Self = D18<SCALE>::ZERO

Source§

const ONE: Self = D18<SCALE>::ONE

Source§

const MAX: Self = D18<SCALE>::MAX

Source§

const MIN: Self = D18<SCALE>::MIN

Source§

type Storage = i64

Source§

impl<const SCALE: u32> Decimal for D38<SCALE>

Source§

const SCALE: u32 = SCALE

Source§

const MAX_SCALE: u32 = 38

Source§

const ZERO: Self = D38<SCALE>::ZERO

Source§

const ONE: Self = D38<SCALE>::ONE

Source§

const MAX: Self = D38<SCALE>::MAX

Source§

const MIN: Self = D38<SCALE>::MIN

Source§

type Storage = i128

Source§

impl<const SCALE: u32> Decimal for D76<SCALE>

Source§

const SCALE: u32 = SCALE

Source§

const MAX_SCALE: u32 = 76

Source§

const ZERO: Self = D76<SCALE>::ZERO

Source§

const ONE: Self = D76<SCALE>::ONE

Source§

const MAX: Self = D76<SCALE>::MAX

Source§

const MIN: Self = D76<SCALE>::MIN

Source§

type Storage = Int256

Source§

impl<const SCALE: u32> Decimal for D153<SCALE>

Source§

const SCALE: u32 = SCALE

Source§

const MAX_SCALE: u32 = 153

Source§

const ZERO: Self = D153<SCALE>::ZERO

Source§

const ONE: Self = D153<SCALE>::ONE

Source§

const MAX: Self = D153<SCALE>::MAX

Source§

const MIN: Self = D153<SCALE>::MIN

Source§

type Storage = Int512

Source§

impl<const SCALE: u32> Decimal for D307<SCALE>

Source§

const SCALE: u32 = SCALE

Source§

const MAX_SCALE: u32 = 307

Source§

const ZERO: Self = D307<SCALE>::ZERO

Source§

const ONE: Self = D307<SCALE>::ONE

Source§

const MAX: Self = D307<SCALE>::MAX

Source§

const MIN: Self = D307<SCALE>::MIN

Source§

type Storage = Int1024