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
impl Decimal
Sourcepub fn new(coefficient: BigInt, exponent: i64) -> Result<Self, Fault>
pub fn new(coefficient: BigInt, exponent: i64) -> Result<Self, 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.
Sourcepub fn parse(spelling: &str) -> Option<Self>
pub fn parse(spelling: &str) -> Option<Self>
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.
Sourcepub fn coefficient(&self) -> &BigInt
pub fn coefficient(&self) -> &BigInt
Returns the coefficient of the normalized form.
Sourcepub fn add(&self, other: &Decimal) -> Result<Decimal, Fault>
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.
Sourcepub fn sub(&self, other: &Decimal) -> Result<Decimal, Fault>
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.
Sourcepub fn mul(&self, other: &Decimal) -> Result<Decimal, Fault>
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.
Sourcepub fn div(&self, divisor: &Decimal) -> Result<Decimal, Fault>
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.
Sourcepub fn floor_within(&self, max_bits: u64) -> Option<Int>
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.
Trait Implementations§
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).
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).
impl Eq for Decimal
Source§impl Ord for Decimal
Numbers by value (data.md).
impl Ord for Decimal
Numbers by value (data.md).