Hi! I'd like feedback on exact, a small set of Rust crates for financial math.

Repo: GitHub - Wandalen/exact · GitHub

The problem

  • 0.1 + 0.2 isn't 0.3 with floats, and in a market those errors add up.
  • Splitting $100 three ways leaves one cent, and someone has to decide who gets it.
  • Prices, quantities and money are all "just numbers", so they get mixed up by mistake.

What exact does

  • Exact: stores whole cents/satoshis, so 0.1 + 0.2 is exactly 0.3.
  • Safe: money, quantity and price are different types; mixing them doesn't compile.
  • Checked: checked operations report overflow instead of wrapping, and a quantity can never go below zero.
  • No lost cents: you choose where a split's leftover goes, and an audit verifies that recorded transfers net to zero for each asset.
  • Snap to the market grid: prices round onto the tick size and quantities onto the lot size (tick 0.05: 0.17 → 0.15).
  • Safe to send and store: a fixed 10-byte format that carries the amount, its precision and its kind, so a cent amount can't be read back as satoshis.
  • Lightweight: 8-byte values, allocation-free core arithmetic, no external dependencies.
exact 0.1 + 0.2 = 0.3 f64 0.1 + 0.2 = 0.30000000000000004 split 100.00 into 3 -> [33.34, 33.33, 33.33] (sum is exactly 100.00) 

Compared to existing crates

Crates like rust_decimal already do exact decimal arithmetic well. The goal is to combine them in one small crate family: separate money/quantity/price types, a leftover-cent policy, a zero-sum audit and tick/lot snapping.

Questions

Are there any common money pitfalls I'm missing?

Thanks!

1 post - 1 participant

Read full topic