Skip to main content

Decimal

Struct Decimal 

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

An exact decimal number.

A value is coefficient × 10^exponent, held normalized: a nonzero coefficient is not a multiple of ten, and zero is 0 × 10^0. That makes the representation a function of the number, so 1.50 and 1.5 are one value with one text and the derived equality and hash are the number’s. Zero has no sign, because a BigInt zero has none.

Addition, subtraction, multiplication and negation are exact. Division is exact when the quotient has a finite expansion and is otherwise rounded to DIVISION_DIGITS significant digits, half to even.

The exponent is a signed 64-bit integer. A result whose exponent would leave that range is reported as Fault::Limit: the number exists, but this host cannot represent its scale.

Implementations§

Source§

impl Decimal

Source

pub fn new(coefficient: BigInt, exponent: i64) -> Result<Decimal, Fault>

Returns the number coefficient × 10^exponent, normalized.

Normalizing raises the exponent once per trailing zero stripped, and a number whose normalized exponent would pass i64::MAX is Fault::Limit.

§Errors

Fault::Limit when the normalized exponent would pass i64::MAX.

Source

pub fn parse(spelling: &str) -> Option<Decimal>

Returns the number a decoded literal spells: ASCII digits with an optional leading -, then optionally . and at least one digit.

This reads what flow-ir writes for a decimal literal; +, an exponent, separators and a bare point are refused.

Source

pub fn coefficient(&self) -> &BigInt

Returns the coefficient of the normalized form.

Source

pub fn exponent(&self) -> i64

Returns the power of ten of the normalized form.

Source

pub fn is_zero(&self) -> bool

Returns whether this is zero.

Source

pub fn add(&self, other: &Decimal) -> Result<Decimal, Fault>

Returns self + other, exactly.

The two are aligned to the lower exponent first, so the sum of numbers whose scales are far apart is written out in full, as exactness requires.

§Errors

Fault::Limit when the result’s exponent would leave the i64 range.

Source

pub fn sub(&self, other: &Decimal) -> Result<Decimal, Fault>

Returns self - other, exactly.

§Errors

Fault::Limit when the result’s exponent would leave the i64 range.

Source

pub fn neg(&self) -> Decimal

Returns -self.

Source

pub fn mul(&self, other: &Decimal) -> Result<Decimal, Fault>

Returns self * other, exactly.

§Errors

Fault::Limit when the result’s exponent would leave the i64 range.

Source

pub fn div(&self, divisor: &Decimal) -> Result<Decimal, Fault>

Returns self / divisor.

A quotient with a finite decimal expansion is exact. Any other is rounded to DIVISION_DIGITS significant digits, half to even. A quotient with no finite expansion is never exactly halfway between two such neighbours — a halfway point has a finite expansion — so rounding to nearest decides every case and the tie rule never has to choose.

§Errors

Fault::DivisionByZero for a zero divisor, and Fault::Limit when the quotient’s exponent would leave the i64 range.

Source

pub fn floor(&self) -> Int

Returns the greatest integer not above this number.

Source

pub fn floor_within(&self, max_bits: u64) -> Option<Int>

Returns Decimal::floor when its magnitude takes at most max_bits bits, and None otherwise.

A number with d digits before the point has a floor of at least 10^(d-1), which takes more than d - 1 bits, so a number too large is refused before its floor is built: a huge exponent costs nothing.

Source

pub fn text_len(&self) -> u128

Returns how many bytes this number’s text takes, without writing it: a number with a vast exponent has a text far too long to build.

Trait Implementations§

Source§

impl Clone for Decimal

Source§

fn clone(&self) -> Decimal

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 Debug for Decimal

Source§

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

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

impl Display for Decimal

The text form: plain decimal notation with no exponent, no trailing zero after the point, no point when the number is whole, - before a negative number and 0 for zero (data.md, Decimal).

Source§

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

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

impl Eq for Decimal

Source§

impl From<&Int> for Decimal

Source§

fn from(value: &Int) -> Decimal

Converts to this type from the input type.
Source§

impl Hash for Decimal

Source§

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

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 Ord for Decimal

Numbers by value (data.md).

Source§

fn cmp(&self, other: &Decimal) -> 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 Decimal

Source§

fn eq(&self, other: &Decimal) -> 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 Decimal

Source§

fn partial_cmp(&self, other: &Decimal) -> 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 Shaped for Decimal

Source§

const FORM: WireForm = WireForm::Decimal

How a value of it is written.
Source§

fn write_at( &self, out: &mut ValueWriter<'_, '_>, ty: &Type, ) -> Result<(), EncodeError>

Writes it as a value of ty, the Flow type its form projects to in out’s catalog. Read more
Source§

fn read_at( input: &mut ValueReader<'_, '_, '_>, ty: &Type, ) -> Result<Decimal, DecodeError>

Reads one, a value of ty in input’s catalog. Read more
Source§

impl StructuralPartialEq for Decimal

Auto Trait Implementations§

§

impl Forget for Decimal

§

impl Freeze for Decimal
where *const u8 is !null: Freeze, PhantomData<u64>: Freeze,

§

impl Move for Decimal

§

impl RefUnwindSafe for Decimal

§

impl Send for Decimal
where RawVecInner: Send,

§

impl Sync for Decimal
where RawVecInner: Sync,

§

impl Unpin for Decimal
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Decimal

§

impl UnwindSafe for Decimal
where RawVecInner: UnwindSafe,

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> Part for T
where T: Shaped,

Source§

fn write_part( &self, out: &mut ValueWriter<'_, '_>, ty: &Type, ) -> Result<(), EncodeError>

Writes it as a value of ty, as Shaped::write_at does. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.