| Crates.io | nvana_fixed |
| lib.rs | nvana_fixed |
| version | 0.1.0 |
| created_at | 2025-12-20 00:22:54.877564+00 |
| updated_at | 2025-12-20 00:22:54.877564+00 |
| description | High-precision fixed-point arithmetic library for financial and DeFi calculations |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1995793 |
| size | 51,749 |
A high-precision fixed-point arithmetic library for Rust, designed for financial and DeFi calculations where precision is critical.
Option on overflow/underflowAdd this to your Cargo.toml:
[dependencies]
nvana_fixed = "0.1.0"
use nvana_fixed::PreciseNumber;
// Basic arithmetic
let a = PreciseNumber::new(10).unwrap();
let b = PreciseNumber::new(3).unwrap();
let sum = a.checked_add(&b).unwrap();
let product = a.checked_mul(&b).unwrap();
let quotient = a.checked_div(&b).unwrap();
assert_eq!(sum.to_imprecise().unwrap(), 13);
assert_eq!(product.to_imprecise().unwrap(), 30);
assert_eq!(quotient.to_imprecise().unwrap(), 3); // Rounded
// Working with percentages
let rate = PreciseNumber::from_basis_points(500).unwrap(); // 5%
let principal = PreciseNumber::new(1000).unwrap();
let interest = principal.checked_mul(&rate).unwrap();
assert_eq!(interest.to_imprecise().unwrap(), 50);
// Comparisons
assert!(a < b.checked_mul(&PreciseNumber::new(2).unwrap()).unwrap());
PreciseNumber uses a fixed-point representation with 18 decimal places. Internally, values are stored as value * 10^18 in a 256-bit integer:
1.0 is stored as 1_000_000_000_000_000_0000.5 is stored as 500_000_000_000_000_00042.123 is stored as 42_123_000_000_000_000_000All arithmetic operations include automatic rounding correction to minimize truncation errors.
All arithmetic operations return Option<PreciseNumber> and will return None on:
This ensures your code can handle edge cases gracefully.
Licensed under MIT.