Skip to main content

DynDecimal

Trait DynDecimal 

Source
pub trait DynDecimal: 'static {
Show 25 methods // Required methods fn width(&self) -> DecimalWidth; fn scale_dyn(&self) -> u32; fn max_scale(&self) -> u32; fn raw_storage(&self) -> RawStorage; fn as_any(&self) -> &dyn Any; fn clone_box(&self) -> Box<dyn DynDecimal>; fn is_zero(&self) -> bool; fn is_one(&self) -> bool; fn is_positive(&self) -> bool; fn is_negative(&self) -> bool; fn signum(&self) -> Box<dyn DynDecimal>; fn abs(&self) -> Box<dyn DynDecimal>; fn neg(&self) -> Box<dyn DynDecimal>; fn add(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>; fn sub(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>; fn mul(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>; fn div(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>; fn rem(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>; fn rescale_to(&self, target_scale: u32) -> Option<Box<dyn DynDecimal>>; fn rescale_to_with( &self, target_scale: u32, mode: RoundingMode, ) -> Option<Box<dyn DynDecimal>>; fn eq_dyn(&self, rhs: &dyn DynDecimal) -> bool; fn cmp_dyn(&self, rhs: &dyn DynDecimal) -> Option<Ordering>; fn display(&self) -> String; fn to_f64(&self) -> f64; fn to_int(&self) -> i64;
}
Expand description

Object-safe, width-erased view of a decimal value.

Implemented by every concrete Dxx<S> shipped with the crate. See the module-level documentation for the cross-width / cross-scale semantics and the cost model.

The trait is intentionally narrower than crate::Decimal: it covers identity, sign, comparison, arithmetic, rescale, the float bridge, and Display. Transcendental functions and constants live only on the typed surface — use DynDecimal::as_any to downcast to a concrete Dxx<S> and call them there.

Required Methods§

Source

fn width(&self) -> DecimalWidth

Returns the storage tier this value lives in.

Source

fn scale_dyn(&self) -> u32

Returns the decimal scale of this value.

Source

fn max_scale(&self) -> u32

Returns the maximum legal scale for this value’s width.

Source

fn raw_storage(&self) -> RawStorage

Returns the raw storage integer (scale stripped).

Source

fn as_any(&self) -> &dyn Any

Returns this value as a &dyn Any for downcasting to a concrete Dxx<S>.

Source

fn clone_box(&self) -> Box<dyn DynDecimal>

Heap-clones into a fresh Box<dyn DynDecimal>.

Source

fn is_zero(&self) -> bool

Returns true if this value is the additive identity for its type.

Source

fn is_one(&self) -> bool

Returns true if this value is the multiplicative identity for its type.

Source

fn is_positive(&self) -> bool

Returns true if self > 0.

Source

fn is_negative(&self) -> bool

Returns true if self < 0.

Source

fn signum(&self) -> Box<dyn DynDecimal>

Returns +1, 0, or -1 (each at the same width/scale as self).

Source

fn abs(&self) -> Box<dyn DynDecimal>

Returns |self|.

Source

fn neg(&self) -> Box<dyn DynDecimal>

Returns -self.

Source

fn add(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>

self + rhs. Returns None if widths differ, if the auto-rescale to the wider scale overflows, or if the sum overflows.

Source

fn sub(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>

self - rhs. Same width / overflow contract as Self::add.

Source

fn mul(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>

self * rhs. Same width / overflow contract as Self::add.

Source

fn div(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>

self / rhs. Returns None on width mismatch, rescale overflow, product overflow, or division by zero.

Source

fn rem(&self, rhs: &dyn DynDecimal) -> Option<Box<dyn DynDecimal>>

self % rhs. Same contract as Self::div.

Source

fn rescale_to(&self, target_scale: u32) -> Option<Box<dyn DynDecimal>>

Rescale to target_scale using the crate-default rounding mode. Returns None if target_scale > max_scale() or the scale-up multiplication overflows.

Source

fn rescale_to_with( &self, target_scale: u32, mode: RoundingMode, ) -> Option<Box<dyn DynDecimal>>

Rescale with an explicit rounding mode. See Self::rescale_to.

Source

fn eq_dyn(&self, rhs: &dyn DynDecimal) -> bool

Equality after width check + lossless rescale to the wider scale. Different widths are never equal.

Source

fn cmp_dyn(&self, rhs: &dyn DynDecimal) -> Option<Ordering>

Ordering after width check + lossless rescale to the wider scale. Different widths return None.

Source

fn display(&self) -> String

Canonical decimal string. Equivalent to format!("{}", self).

Source

fn to_f64(&self) -> f64

Lossy conversion to f64. Available only with the std feature.

Source

fn to_int(&self) -> i64

Conversion to i64 using the crate-default rounding mode. Saturates on overflow; see crate::DecimalConvert::to_int.

Implementors§

Source§

impl<const SCALE: u32> DynDecimal for D9<SCALE>

Source§

impl<const SCALE: u32> DynDecimal for D18<SCALE>

Source§

impl<const SCALE: u32> DynDecimal for D38<SCALE>