pub struct Decimal {
pub num_digits: usize,
pub decimal_point: i32,
pub truncated: bool,
pub digits: [u8; 768],
}
dec2flt
)Fields§
§num_digits: usize
dec2flt
)The number of significant digits in the decimal.
decimal_point: i32
dec2flt
)The offset of the decimal point in the significant digits.
truncated: bool
dec2flt
)If the number of significant digits stored in the decimal is truncated.
digits: [u8; 768]
dec2flt
)Buffer of the raw digits, in the range [0, 9].
Implementations§
source§impl Decimal
impl Decimal
sourcepub const MAX_DIGITS: usize = 768usize
🔬This is a nightly-only experimental API. (dec2flt
)
pub const MAX_DIGITS: usize = 768usize
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))
sourcepub const MAX_DIGITS_WITHOUT_OVERFLOW: usize = 19usize
🔬This is a nightly-only experimental API. (dec2flt
)
pub const MAX_DIGITS_WITHOUT_OVERFLOW: usize = 19usize
dec2flt
)The max digits that can be exactly represented in a 64-bit integer.
pub const DECIMAL_POINT_RANGE: i32 = 2_047i32
dec2flt
)sourcepub fn try_add_digit(&mut self, digit: u8)
🔬This is a nightly-only experimental API. (dec2flt
)
pub fn try_add_digit(&mut self, digit: u8)
dec2flt
)Append a digit to the buffer.
sourcepub fn trim(&mut self)
🔬This is a nightly-only experimental API. (dec2flt
)
pub fn trim(&mut self)
dec2flt
)Trim trailing zeros from the buffer.
pub fn round(&self) -> u64
dec2flt
)sourcepub fn left_shift(&mut self, shift: usize)
🔬This is a nightly-only experimental API. (dec2flt
)
pub fn left_shift(&mut self, shift: usize)
dec2flt
)Computes decimal * 2^shift.
sourcepub fn right_shift(&mut self, shift: usize)
🔬This is a nightly-only experimental API. (dec2flt
)
pub fn right_shift(&mut self, shift: usize)
dec2flt
)Computes decimal * 2^-shift.