Chaitanya Bharath Gopu · IEEE Senior Member · ORCID 0009-0004-2511-3850

Two systems say they billed the same customer the same amount. One export says 0.30, the other says 0.30000000000000004. A diff script flags it, someone adds abs(a-b) < 1e-9, and the problem moves from "visible" to "folklore".

This post covers where that comes from, the two exact ways to avoid it (Decimal and integer minor units), the design of a small open-source ledger diff, decimal-ledger-diff, and the properties that matter more than the arithmetic: determinism, loud failure and honest limits.

What goes wrong with f64

IEEE-754 binary floating point cannot represent most base-10 fractions. These are not edge cases; they are the first things you try:

0.1 + 0.2 == 0.3 -> false (0.30000000000000004) ten additions of 0.1 -> 0.9999999999999999 

Over volume it adds up. In the benchmark that ships with the tool, adding one cent a million times:

sum of 1,000,000 x 0.01: f64 = 10000.0000001719 (error +1.7e-7) Decimal = 10000.00 (exact) 

A drift of 1.7e-7 on ten thousand dollars is invisible in a report and fatal in an equality check, which is what reconciliation is.

Two exact answers

Integer minor units. Store cents (or the currency's smallest unit) in an i64. Addition is exact, comparison is trivial, it is very fast. The cost: you must fix one scale per currency up front, and anything with sub-cent precision (usage-based cloud billing, FX, per-token pricing) needs a wider scale or a different type. I use this approach in a separate reconciliation engine where every source is a single-currency bank or processor feed.

Base-10 decimal. rust_decimal stores an integer mantissa plus a decimal scale. It represents 0.1, 0.01 and 19.99999 exactly, carries its own scale, and compares numerically (1.0 == 1.000). The cost: some speed, and you must still choose a rounding mode when you divide or convert.

Rule of thumb I use: integer cents when the data is a single-currency ledger with a known scale; Decimal when scales vary or sub-cent amounts are real (cloud usage lines, taxes, amortised credits). decimal-ledger-diff targets the second case.

What the diff guarantees

Exact arithmetic is necessary, not sufficient. Reconciliation tools fail in more boring ways, so these are the properties I designed and tested for:

  1. Order independence. Rows are indexed by id and output is sorted by id. Shuffle either input and you get the same report, byte for byte, and the same SHA-256 fingerprint. That fingerprint is what makes "we reconciled this month's file" checkable later.
  2. Loud parsing. Amounts accept $1,200.10, €, £, a leading sign and accounting-style (45.00) negatives. Anything else ("", abc, 1.2.3, 12x, --5) is an error with a row number, never a silent zero. A silent zero is how a billing export with a corrupt line "reconciles".
  3. Duplicates are findings, not noise. A repeated id is reported on its side of the diff. It is never summed or overwritten silently.
  4. Scale-insensitive equality. 1.0 and 1.000 match, because they are the same number.
  5. Explicit tolerance. --tolerance is an absolute, inclusive amount (default 0), so any slack is a visible decision, not a hidden epsilon.

The core is small enough to read:

pub fn diff(left: &[Entry], right: &[Entry], tolerance: Decimal) -> Report { let (l, dups_l) = index(left); // BTreeMap<&str, Decimal>, so iteration is sorted let (r, dups_r) = index(right); // matched / mismatched{delta} / only_left / only_right, then Report::fingerprint() } 

A sample run (two tiny CSVs in the repo):

{"report":{"matched":2, "mismatched":[{"id":"inv-003","left":"-45.00","right":"-45.01","delta":"0.01"}], "only_left":["inv-004"],"only_right":["inv-005"], "duplicate_ids_left":[],"duplicate_ids_right":[]}, "sha256":"005b0265…"} 

Exit codes make it scriptable: 0 clean, 1 differences, 64 input error.

Measurements

From cargo run --release --example bench on an Intel i9-13950HX (32 GB, Windows 11, rustc 1.97.1). Inputs are generated, the right side is in reverse order, and 1% of rows differ by one cent.

rows time mismatches found 10,000 4.2 ms 100 100,000 42.8 ms 1,000 1,000,000 488 ms 10,000 

That is roughly 2 million rows per second including building the id index on both sides, in memory, single-threaded. It scales linearly in this range. I have not measured memory, and I am not comparing it against Python or Excel; throughput is not the point of the tool. Treat these numbers as "fast enough that the CPU is not your bottleneck".

Tests

Nine tests (cargo test): f64 drift versus Decimal asserted on both sides, money-format parsing, garbage rejection, scale-insensitive equality, all four difference kinds, tolerance boundaries (0.01 passes, 0.009 fails), duplicate reporting, order-independence of both report and fingerprint, and the bundled sample files.

What Decimal does not fix

Choosing an exact type removes binary representation error and nothing else. Before reconciliation is trustworthy you still need to agree, between systems, on:

  • Currency and scale. Is 100 dollars or cents? Mismatched units produce confident, exact, wrong answers.
  • Rounding mode. Half-up, half-even (banker's) or truncation, applied at the same step in both systems.
  • Normalisation. Date formats, id canonicalisation (INV-001 vs inv-001), and one-to-many relationships (a payout covering many charges). This tool matches one row per id; it does no many-to-one or fuzzy matching and no FX.
  • Controls. Exact arithmetic is a building block, not a compliance posture. Access control, change management and review of exceptions are separate work.

Try it

git clone https://github.com/gchaitanyabharath9/decimal-ledger-diff && cd decimal-ledger-diff cargo test cargo run --release -- samples/left.csv samples/right.csv --tolerance 0.00 cargo run --release --example bench 

Takeaways

  • f64 for money fails equality checks first and accuracy second. Use integer minor units or Decimal, and say which and why.
  • The properties that make a reconciliation trustworthy are order independence, loud failure on bad input, visible tolerance, and a fingerprint of the result.
  • Publish drift and throughput with hardware and method. A benchmark you can rerun is worth more than an adjective.

Chaitanya Bharath Gopu is a Principal Enterprise Architect and AI systems researcher (19+ years in enterprise cloud modernization, FinOps and Rust/WASM systems) and an IEEE Senior Member. Code: github.com/gchaitanyabharath9 · Datasets: huggingface.co/CHAITANYABHARATHGOPU