precision-core
Deterministic fixed-point arithmetic for financial computation.
Features
- 128-bit decimal arithmetic with up to 28 significant digits
no_stdcompatible for embedded and WASM targets- 7 rounding modes including banker's rounding
- Transcendental functions (exp, ln, sqrt, pow)
- Oracle integration utilities (Chainlink, Pyth)
- Deterministic results across all platforms
- Zero unsafe code
Installation
[]
= "0.1"
Quick Start
use ;
// From integers
let a = from;
// From mantissa and scale: value = mantissa * 10^(-scale)
let b = new; // 123.45
// Checked arithmetic
let sum = a.checked_add.unwrap;
let product = a.checked_mul.unwrap;
// Rounding
let rounded = b.round; // 123.5
// Transcendental functions
let sqrt = from.try_sqrt.unwrap; // ~1.414...
let exp = ONE.try_exp.unwrap; // ~2.718...
let ln = from.try_ln.unwrap; // ~2.302...
Oracle Integration
use ;
// Chainlink BTC/USD (8 decimals)
let btc_raw = 5000012345678i64; // $50,000.12345678
let btc_price = normalize_oracle_price?;
// Convert between decimal formats
use convert_decimals;
let usdc_amount = convert_decimals?;
Rounding Modes
| Mode | Description |
|---|---|
HalfEven |
Banker's rounding (default) |
HalfUp |
Traditional rounding |
HalfDown |
Ties toward zero |
Up |
Toward +infinity |
Down |
Toward -infinity |
TowardZero |
Truncation |
AwayFromZero |
Away from zero |
no_std Usage
The crate is no_std by default. Enable the std feature for standard library support:
[]
= { = "0.1", = ["std"] }
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.