pub struct Decimal(/* private fields */);Expand description
A 128-bit decimal number with deterministic arithmetic.
This type wraps rust_decimal::Decimal and provides checked arithmetic
operations that explicitly handle overflow, underflow, and division by zero.
All operations are deterministic across platforms.
Implementations§
Source§impl Decimal
impl Decimal
Sourcepub const NEGATIVE_ONE: Self
pub const NEGATIVE_ONE: Self
Negative one.
Sourcepub const ONE_HUNDRED: Self
pub const ONE_HUNDRED: Self
One hundred.
Sourcepub const ONE_THOUSAND: Self
pub const ONE_THOUSAND: Self
One thousand.
Sourcepub fn new(mantissa: i64, scale: u32) -> Self
pub fn new(mantissa: i64, scale: u32) -> Self
Creates a new decimal from integer mantissa and scale.
The value is mantissa * 10^(-scale).
§Panics
Panics if scale exceeds 28.
Sourcepub const fn from_parts(
lo: u32,
mid: u32,
hi: u32,
negative: bool,
scale: u32,
) -> Self
pub const fn from_parts( lo: u32, mid: u32, hi: u32, negative: bool, scale: u32, ) -> Self
Creates a decimal from raw parts.
The 96-bit mantissa is stored as three 32-bit words in little-endian order.
The sign is true for negative values.
Sourcepub fn try_from_i128(value: i128) -> Result<Self, ArithmeticError>
pub fn try_from_i128(value: i128) -> Result<Self, ArithmeticError>
Creates a decimal from a 128-bit integer.
Returns an error if the value is too large to represent.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Returns true if the value is negative.
Sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Returns true if the value is positive.
Sourcepub fn checked_add(self, other: Self) -> Option<Self>
pub fn checked_add(self, other: Self) -> Option<Self>
Checked addition. Returns None on overflow.
Sourcepub fn checked_sub(self, other: Self) -> Option<Self>
pub fn checked_sub(self, other: Self) -> Option<Self>
Checked subtraction. Returns None on overflow.
Sourcepub fn checked_mul(self, other: Self) -> Option<Self>
pub fn checked_mul(self, other: Self) -> Option<Self>
Checked multiplication. Returns None on overflow.
Sourcepub fn checked_div(self, other: Self) -> Option<Self>
pub fn checked_div(self, other: Self) -> Option<Self>
Checked division. Returns None on division by zero or overflow.
Sourcepub fn checked_rem(self, other: Self) -> Option<Self>
pub fn checked_rem(self, other: Self) -> Option<Self>
Checked remainder. Returns None on division by zero.
Sourcepub fn saturating_add(self, other: Self) -> Self
pub fn saturating_add(self, other: Self) -> Self
Saturating addition. Returns MAX or MIN on overflow.
Sourcepub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
Saturating subtraction. Returns MAX or MIN on overflow.
Sourcepub fn saturating_mul(self, other: Self) -> Self
pub fn saturating_mul(self, other: Self) -> Self
Saturating multiplication. Returns MAX or MIN on overflow.
Sourcepub fn try_add(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_add(self, other: Self) -> Result<Self, ArithmeticError>
Addition with explicit error on overflow.
Sourcepub fn try_sub(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_sub(self, other: Self) -> Result<Self, ArithmeticError>
Subtraction with explicit error on overflow.
Sourcepub fn try_mul(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_mul(self, other: Self) -> Result<Self, ArithmeticError>
Multiplication with explicit error on overflow.
Sourcepub fn try_div(self, other: Self) -> Result<Self, ArithmeticError>
pub fn try_div(self, other: Self) -> Result<Self, ArithmeticError>
Division with explicit error handling.
Sourcepub fn round(self, dp: u32, mode: RoundingMode) -> Self
pub fn round(self, dp: u32, mode: RoundingMode) -> Self
Rounds to the specified number of decimal places using the given mode.
Sourcepub fn round_dp(self, dp: u32) -> Self
pub fn round_dp(self, dp: u32) -> Self
Rounds to the specified number of decimal places using banker’s rounding.
Sourcepub fn rescale(&mut self, scale: u32) -> Result<(), ArithmeticError>
pub fn rescale(&mut self, scale: u32) -> Result<(), ArithmeticError>
Rescales to the specified number of decimal places.
Returns an error if the scale would exceed MAX_SCALE.
Sourcepub fn into_inner(self) -> RustDecimal
pub fn into_inner(self) -> RustDecimal
Returns the internal representation for interop.
Sourcepub fn from_inner(inner: RustDecimal) -> Self
pub fn from_inner(inner: RustDecimal) -> Self
Creates from the internal representation.
Sourcepub fn sqrt(self) -> Option<Self>
pub fn sqrt(self) -> Option<Self>
Computes the square root.
Returns None if the value is negative.
§Example
use precision_core::Decimal;
let x = Decimal::from(4i64);
assert_eq!(x.sqrt(), Some(Decimal::from(2i64)));
let neg = Decimal::from(-1i64);
assert_eq!(neg.sqrt(), None);Sourcepub fn try_sqrt(self) -> Result<Self, ArithmeticError>
pub fn try_sqrt(self) -> Result<Self, ArithmeticError>
Computes the square root, returning an error for negative inputs.
Sourcepub fn exp(self) -> Option<Self>
pub fn exp(self) -> Option<Self>
Computes e^self (the exponential function).
Returns None on overflow.
§Example
use precision_core::Decimal;
let x = Decimal::ZERO;
assert_eq!(x.exp(), Some(Decimal::ONE));Sourcepub fn try_exp(self) -> Result<Self, ArithmeticError>
pub fn try_exp(self) -> Result<Self, ArithmeticError>
Computes e^self, returning an error on overflow.
Sourcepub fn ln(self) -> Option<Self>
pub fn ln(self) -> Option<Self>
Computes the natural logarithm (ln).
Returns None if the value is not positive.
§Example
use precision_core::Decimal;
use core::str::FromStr;
let e = Decimal::from_str("2.718281828459045").unwrap();
let ln_e = e.ln().unwrap();
// ln(e) ≈ 1
assert!((ln_e - Decimal::ONE).abs() < Decimal::from_str("0.0001").unwrap());Sourcepub fn try_ln(self) -> Result<Self, ArithmeticError>
pub fn try_ln(self) -> Result<Self, ArithmeticError>
Computes the natural logarithm, returning an error for non-positive inputs.
Sourcepub fn log10(self) -> Option<Self>
pub fn log10(self) -> Option<Self>
Computes the base-10 logarithm.
Returns None if the value is not positive.
Sourcepub fn pow(self, exponent: Self) -> Option<Self>
pub fn pow(self, exponent: Self) -> Option<Self>
Computes self^exponent using the formula: x^y = e^(y * ln(x)).
Returns None if the computation would fail (e.g., negative base with
non-integer exponent, or overflow).
Note: For integer exponents, use powi for exact results.
§Example
use precision_core::Decimal;
use core::str::FromStr;
let base = Decimal::from(2i64);
let exp = Decimal::from(3i64);
let result = base.pow(exp).unwrap();
// Note: small precision loss due to exp/ln computation
let diff = (result - Decimal::from(8i64)).abs();
assert!(diff < Decimal::from_str("0.001").unwrap());Sourcepub fn try_pow(self, exponent: Self) -> Result<Self, ArithmeticError>
pub fn try_pow(self, exponent: Self) -> Result<Self, ArithmeticError>
Computes self^exponent, returning an error on failure.
Sourcepub fn powi(self, n: i32) -> Option<Self>
pub fn powi(self, n: i32) -> Option<Self>
Computes self^n for integer exponent using repeated squaring.
This is more accurate than pow() for integer exponents as it avoids
the exp/ln computation. Returns None on overflow.
§Example
use precision_core::Decimal;
let base = Decimal::from(2i64);
assert_eq!(base.powi(10), Some(Decimal::from(1024i64)));Sourcepub fn try_powi(self, n: i32) -> Result<Self, ArithmeticError>
pub fn try_powi(self, n: i32) -> Result<Self, ArithmeticError>
Computes self^n for integer exponent, returning error on failure.