Exact Decimal Arithmetic
Floating-point numbers (f32, f64) use base-2 binary fractions, causing representation artifacts in base-10 financial math (e.g., ).
SabiQuant provides DecimalAmount, a 128-bit high-integrity fixed-point arithmetic engine designed specifically for accounting, clearing, and currency settlements.
Key Specifications
- Internal Representation: 128-bit signed integer with fixed or scaled decimal exponent.
- Precision: Up to 38 decimal digits of exact precision.
- Zero Allocations: Stack-allocated value type with copy semantics.
- Rounding Modes: 7 standard IEEE 754 / ISO rounding strategies:
HalfEven(Banker’s rounding - default in institutional finance)HalfUp(Round half towards positive infinity)HalfDown(Round half towards zero)Ceiling(Round towards )Floor(Round towards )Up(Round away from zero)Down(Truncate towards zero)
Rust Code Example
use sabiquant::exact::{DecimalAmount, RoundingMode};
fn main() -> Result<(), Box<dyn std::error::Error>> { // Parsing exact decimal strings let price = DecimalAmount::from_str("102.435000000000000000")?; let factor = DecimalAmount::from_str("0.045000000000000000")?;
// Exact multiplication let gross = price.checked_mul(&factor)?; println!("Exact Gross: {}", gross);
// Rounding to 2 decimal places for cash settlement let rounded_bankers = gross.round(2, RoundingMode::HalfEven)?; let rounded_up = gross.round(2, RoundingMode::Up)?;
println!("Half-Even (Banker's): {}", rounded_bankers); println!("Round Up: {}", rounded_up);
// Safe comparison without epsilon jitter assert!(rounded_bankers <= rounded_up);
Ok(())}