pub struct PyDec { /* private fields */ }Expand description
A decimal.Decimal, modelled the way CPython models it: a sign flag, a
non-negative integer coefficient, and a base-10 exponent.
Only the operations _parse performs are ported — construction from an
int, construction from "0." + digits, addition of two non-negative
values, and multiplication by ±1. Rounding is ROUND_HALF_EVEN at
PREC significant digits, matching the default context.
The context’s Emax/Emin/clamp machinery is not ported because this
grammar cannot reach it: the largest scale word is centillion (10^303)
and _cardinal_value adds scales rather than multiplying them, so a
coefficient of ~10^999972 (what Etop would require) is unreachable, and
a pure fraction always lands at exp_min == -PREC.
Implementations§
Source§impl PyDec
impl PyDec
Sourcepub fn to_bigdecimal(&self) -> BigDecimal
pub fn to_bigdecimal(&self) -> BigDecimal
The value as a BigDecimal.
Lossy for a negative zero — BigDecimal has no such thing, so
Decimal('-0.0') arrives as 0.0. Use to_string() when that
distinction matters.
Sourcepub fn from_bigdecimal(d: &BigDecimal) -> PyDec
pub fn from_bigdecimal(d: &BigDecimal) -> PyDec
The inverse of [to_bigdecimal] — build a PyDec from a BigDecimal.
BigDecimal has no signed zero, so a value it carries always maps to a
non-negative-zero PyDec; that is exactly right here, because the only
caller feeds it reverse-table results (int/float promoted through a
Dec arm), never a genuine grammar-produced signed zero.
Trait Implementations§
Source§impl Display for PyDec
Port of Decimal.__str__ (the spec’s to-scientific-string) with the
default context’s capitals=1 and eng=False.
impl Display for PyDec
Port of Decimal.__str__ (the spec’s to-scientific-string) with the
default context’s capitals=1 and eng=False.
This is not BigDecimal’s Display: Python prints 9.99…E+305 where
BigDecimal prints 999e+303, and Python prints 0.0 for a zero with
exponent -1 where BigDecimal prints 0.