fermat-core 0.1.1

128-bit fixed-point arithmetic for Solana sBPF — no_std, zero dependencies
Documentation
  • Coverage
  • 100%
    58 out of 58 items documented1 out of 28 items with examples
  • Size
  • Source code size: 103.22 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.1 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • XXIX-labs/fermat-math
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • kunal-drall

fermat-core

128-bit fixed-point decimal arithmetic for Solana's sBPF runtime.

fermat-core is the core arithmetic crate of the fermat-math project. It provides a Decimal type backed by an i128 mantissa and u8 scale, with checked arithmetic, 7 IEEE 754-2008 rounding modes, and a 256-bit intermediate for overflow-safe mul_div.

crates.io docs.rs License: MIT OR Apache-2.0

Features

  • #![no_std] — compiles to bare-metal sBPF
  • #![forbid(unsafe_code)] — no unsafe blocks anywhere
  • Zero external dependencies — minimal binary size
  • Every operation returns Result — no panics, ever
  • 7 IEEE 754-2008 rounding modes — explicit rounding direction
  • checked_mul_div — 256-bit intermediate prevents (a*b)/c overflow
  • 17 bytes on-chain — compact i128 + u8 Borsh encoding

Quick Start

use fermat_core::{Decimal, RoundingMode};

let price  = Decimal::new(150_000_000, 6)?;   // 150.000000
let amount = Decimal::new(2_500_000, 6)?;      //   2.500000

let total  = price.checked_mul(amount)?;
let result = total.round(6, RoundingMode::HalfEven)?;  // 375.000000

// Overflow-safe: (a * b) / c via U256 intermediate
let health = collateral.checked_mul_div(threshold, debt)?;

Installation

[dependencies]
fermat-core = "0.1"

Modules

Module Contents
decimal Decimal struct, constants (ZERO, ONE, MAX, MIN)
arithmetic checked_add/sub/mul/div, checked_mul_div (U256), checked_neg/abs
rounding 7 modes: Down, Up, TowardZero, AwayFromZero, HalfUp, HalfDown, HalfEven
convert from_u64/i64/u128, from_str_exact, to_token_amount
compare Ord/PartialOrd with scale normalisation
display Human-readable Display (e.g. 1.500000)
error ArithmeticError: Overflow, DivisionByZero, ScaleExceeded, InvalidInput

See Also

License

Licensed under either MIT or Apache-2.0 at your option.

Copyright 2026 XXIX Labs