Expand description
§fermat-core
128-bit fixed-point decimal arithmetic for Solana’s sBPF runtime.
§Design Goals
no_std: Works on Solana sBPF without any OS dependencies.- Zero external dependencies: Only
proptestappears as a dev-dependency. - Panic-free: Every fallible operation returns
Result<_, ArithmeticError>. #![forbid(unsafe_code)]: No unsafe blocks anywhere in the crate.- Overflow-safe
mul_div: Uses a 256-bit intermediate (U256) to prevent the class of bugs where(a × b) / csilently wraps whena × boverflowsi128.
§Core Type
Decimal { mantissa: i128, scale: u8 }
value = mantissa × 10^(-scale)The scale is bounded to [0, 28] (MAX_SCALE). On-chain Borsh encoding is
exactly 17 bytes (16 bytes mantissa LE + 1 byte scale).
§Quick Start
use fermat_core::{Decimal, RoundingMode};
let price = Decimal::new(150_000_000, 6).unwrap(); // 150.000000
let amount = Decimal::new(2_500_000, 6).unwrap(); // 2.500000
let total = price.checked_mul(amount).unwrap(); // 375.000000...
let result = total.round(6, RoundingMode::HalfEven).unwrap();Re-exports§
pub use decimal::Decimal;pub use decimal::MAX_SCALE;pub use decimal::SOL_SCALE;pub use decimal::USDC_SCALE;pub use error::ArithmeticError;pub use rounding::RoundingMode;
Modules§
- arithmetic
- Arithmetic operations: add, sub, mul, div, mul_div, neg, abs.
- compare
OrdandPartialOrdforDecimalwith automatic scale normalisation.- convert
- Conversions between
Decimaland primitive types. - decimal
- Core
Decimaltype definition and constants. - display
DisplayandDebugformatting forDecimal.- error
- Error types for fermat-core arithmetic operations.
- rounding
- IEEE 754-2008 rounding modes and the
Decimal::roundmethod.