v147 · JavaScript · Math · demo

Financial Ledger

Add transactions to a ledger and compare the running balance computed by naïve array.reduce() vs Math.sumPrecise(). Financial totals are notoriously sensitive to floating-point drift — this demo shows exactly where and why they diverge.

Chrome 147+Math.sumPrecise(). In older browsers the precise column falls back to a Kahan-compensated approximation and is labelled accordingly.

Add transactions below · watch the "Naive sum" and "sumPrecise" totals drift apart · load the stress-test to see large-scale drift

Transactions
Description Amount (£) Type
Naïve reduce()
£0.00
Math.sumPrecise()
£0.00
// Financial totals: naive vs Math.sumPrecise
const amounts = transactions.map(t =>
  t.type === 'credit' ? t.amount : -t.amount
);

// Naive — accumulates floating-point error
const naive = amounts.reduce((acc, v) => acc + v, 0);

// Math.sumPrecise — exact IEEE 754 result, no drift
const precise = Math.sumPrecise(amounts);

// On a real ledger with hundreds of £0.10 debit entries:
// naive  → £-3.9999999999999982  (off by £1.8e-15)
// precise → £-4.00               (exact)

see also

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗