Struct core::num::dec2flt::decimal::Decimal

source ·
pub struct Decimal {
    pub num_digits: usize,
    pub decimal_point: i32,
    pub truncated: bool,
    pub digits: [u8; 768],
}
🔬This is a nightly-only experimental API. (dec2flt)

Fields§

§num_digits: usize
🔬This is a nightly-only experimental API. (dec2flt)

The number of significant digits in the decimal.

§decimal_point: i32
🔬This is a nightly-only experimental API. (dec2flt)

The offset of the decimal point in the significant digits.

§truncated: bool
🔬This is a nightly-only experimental API. (dec2flt)

If the number of significant digits stored in the decimal is truncated.

§digits: [u8; 768]
🔬This is a nightly-only experimental API. (dec2flt)

Buffer of the raw digits, in the range [0, 9].

Implementations§

source§

impl Decimal

source

pub const MAX_DIGITS: usize = 768usize

🔬This is a nightly-only experimental API. (dec2flt)

The maximum number of digits required to unambiguously round a float.

For a double-precision IEEE 754 float, this required 767 digits, so we store the max digits + 1.

We can exactly represent a float in radix b from radix 2 if b is divisible by 2. This function calculates the exact number of digits required to exactly represent that float.

According to the “Handbook of Floating Point Arithmetic”, for IEEE754, with emin being the min exponent, p2 being the precision, and b being the radix, the number of digits follows as:

−emin + p2 + ⌊(emin + 1) log(2, b) − log(1 − 2^(−p2), b)⌋

For f32, this follows as: emin = -126 p2 = 24

For f64, this follows as: emin = -1022 p2 = 53

In Python: -emin + p2 + math.floor((emin+ 1)*math.log(2, b)-math.log(1-2**(-p2), b))

source

pub const MAX_DIGITS_WITHOUT_OVERFLOW: usize = 19usize

🔬This is a nightly-only experimental API. (dec2flt)

The max digits that can be exactly represented in a 64-bit integer.

source

pub const DECIMAL_POINT_RANGE: i32 = 2_047i32

🔬This is a nightly-only experimental API. (dec2flt)
source

pub fn try_add_digit(&mut self, digit: u8)

🔬This is a nightly-only experimental API. (dec2flt)

Append a digit to the buffer.

source

pub fn trim(&mut self)

🔬This is a nightly-only experimental API. (dec2flt)

Trim trailing zeros from the buffer.

source

pub fn round(&self) -> u64

🔬This is a nightly-only experimental API. (dec2flt)
source

pub fn left_shift(&mut self, shift: usize)

🔬This is a nightly-only experimental API. (dec2flt)

Computes decimal * 2^shift.

source

pub fn right_shift(&mut self, shift: usize)

🔬This is a nightly-only experimental API. (dec2flt)

Computes decimal * 2^-shift.

Trait Implementations§

source§

impl Clone for Decimal

source§

fn clone(&self) -> Decimal

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Default for Decimal

source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> SizedTypeProperties for T

source§

const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.