Skip to main content

DynamicDecimal

Struct DynamicDecimal 

Source
pub struct DynamicDecimal { /* private fields */ }
Expand description

Decimal real number with 38 digits of precision and dynamic scale.

This type is primarily meant as a serialized form of Fixed that does not require parameterization. Any Fixed can be converted into DynamicDecimal and then converted back into any other Fixed, possibly with a different precision and scale, without loss of precision (beyond that inherent in change of scale, if any).

§Representation

The number is represented as an i128 significand and a u8 scale, that together express the value significand * 10**-scale.

§Invariants

These invariants on significand and scale ensure that the value is in canonical form:

  • If significand != 0 && scale != 0, then significand must not be a multiple of 10.

  • If significand != 0 && scale == 0, then significand may be a multiple of 10. (This is a “denormalized” representation with fewer digits of precision than other forms.)

  • If significand == 0, then scale must be zero.

§Arithmetic

There are various arithmetic traits implemented for DynamicDecimal, but they are intended for limited uses; in particular, these functions will panic for overflows.

Implementations§

Source§

impl DynamicDecimal

Source

pub const MAX: Self

The largest DynamicDecimal value (i128::MAX).

Source

pub const MIN: Self

The smallest DynamicDecimal value (i128::MIN).

Source

pub const ZERO: Self

0 as DynamicDecimal.

Source

pub const ONE: Self

1 as DynamicDecimal.

Source

pub const fn new(sig: i128, exponent: u8) -> Self

Constructs a new DynamicDecimal with value sig * 10**-exponent.

Source

pub const fn significand(&self) -> i128

Returns the canonical significand.

Source

pub const fn exponent(&self) -> u8

Returns the canonical scale.

Source

pub fn trunc(&self) -> Self

Truncates the value to an integer by discarding digits after decimal point

Source

pub fn trunc_digits(&self, digits: u8) -> Self

Returns this value, truncated toward zero at digits after the decimal point.

Source

pub fn floor(&self) -> Self

Floor of the value

Source

pub const fn is_negative(self) -> bool

True if value is negative

Source

pub const fn abs(self) -> Self

The absolute value

Trait Implementations§

Source§

impl Add for DynamicDecimal

Source§

type Output = DynamicDecimal

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl CheckedDiv for DynamicDecimal

Source§

fn checked_div(&self, other: &DynamicDecimal) -> Option<DynamicDecimal>

Divides two numbers, checking for underflow, overflow and division by zero. If any of that happens, None is returned.
Source§

impl Clone for DynamicDecimal

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DynamicDecimal

Source§

impl Debug for DynamicDecimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DynamicDecimal

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for DynamicDecimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div for DynamicDecimal

Source§

type Output = DynamicDecimal

The resulting type after applying the / operator.
Source§

fn div(self, other: DynamicDecimal) -> DynamicDecimal

Performs the / operation. Read more
Source§

impl Eq for DynamicDecimal

Source§

impl From<DynamicDecimal> for i128

Source§

fn from(value: DynamicDecimal) -> Self

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Source§

impl From<DynamicDecimal> for f64

Source§

fn from(value: DynamicDecimal) -> Self

Converts to this type from the input type.
Source§

impl From<DynamicDecimal> for f32

Source§

fn from(value: DynamicDecimal) -> Self

Converts to this type from the input type.
Source§

impl<const P: usize, const S: usize> From<Fixed<P, S>> for DynamicDecimal

Source§

fn from(value: Fixed<P, S>) -> Self

Encodes value, for later deserialization into a new Fixed with possibly a different precision and scale.

Source§

impl From<i8> for DynamicDecimal

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for DynamicDecimal

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for DynamicDecimal

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for DynamicDecimal

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for DynamicDecimal

Source§

fn from(value: i128) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for DynamicDecimal

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for DynamicDecimal

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for DynamicDecimal

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for DynamicDecimal

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for DynamicDecimal

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for DynamicDecimal

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl FromStr for DynamicDecimal

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses s as DynamicDecimal.

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 = ParseDecimalError

The associated error which can be returned from parsing.
Source§

impl Hash for DynamicDecimal

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul for DynamicDecimal

Source§

type Output = DynamicDecimal

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl Neg for DynamicDecimal

Source§

type Output = DynamicDecimal

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for DynamicDecimal

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for DynamicDecimal

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for DynamicDecimal

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Rem for DynamicDecimal

Source§

type Output = DynamicDecimal

The resulting type after applying the % operator.
Source§

fn rem(self, other: Self) -> Self

Performs the % operation. Read more
Source§

impl StructuralPartialEq for DynamicDecimal

Source§

impl Sub for DynamicDecimal

Source§

type Output = DynamicDecimal

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl<const P: usize, const S: usize> TryFrom<DynamicDecimal> for Fixed<P, S>

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Deserializes DynamicDecimal into Fixed. If successful, returns the original value from before serialization. If S < value.exponent, then some trailing decimals are lost, by rounding toward zero. Returns an error` if the value is out of range for this type.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for i64

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for i32

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for i16

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for i8

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for isize

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for u128

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for u64

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for u32

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for u16

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for u8

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<DynamicDecimal> for usize

Source§

fn try_from(value: DynamicDecimal) -> Result<Self, Self::Error>

Convert from Fixed to integer, rounding toward zero (the same semantics as Rust casts from float to integer).

Because this rounds toward zero, negative values greater than -1 will convert to 0 instead of an out-of-range error.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<f64> for DynamicDecimal

Source§

fn try_from(value: f64) -> Result<Self, Self::Error>

Convert value to DynamicDecimal, reporting an error if value is out of range.

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

impl TryFrom<u128> for DynamicDecimal

Source§

type Error = OutOfRange

The type returned in the event of a conversion error.
Source§

fn try_from(value: u128) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V