Why we use exact fractions for land and inheritance maths
Banglaghora helps people write Bangla land deeds and includes tools for land units, inheritance (faraiz) shares and registration fees. Early on we made one decision that shaped all of that code: no floating-point numbers for anything that ends up on a legal document. This post explains why, and how we do it instead.
The problem with 0.1 + 0.2
Most programming languages store decimal numbers as binary floating point. It is fast and fine for graphics or physics, but it cannot represent most decimal fractions exactly. The classic example in JavaScript:
0.1 + 0.2 === 0.3 // false
0.1 + 0.2 // 0.30000000000000004
That error looks harmless. On a deed, it is not.
Why land and inheritance make it worse
Land in Bangladesh is measured in units such as shotangsho (decimal), katha and bigha, and conversions between them don’t produce tidy decimals. Inheritance under faraiz splits an estate into fractions like 1/8, 1/6 and 2/3, then splits those shares again among heirs. A single plot can pass through several generations of such divisions.
Each step with floating point adds a tiny rounding error. Repeat the steps and the errors accumulate. Two things then go wrong:
- Shares stop adding up. The heirs’ shares sum to 0.9999999 or 1.0000001 of the land instead of exactly the whole.
- Displayed numbers disagree. Rounding for display hides the error in one place and shows it in another, so the deed and the summary differ in the last digit.
When the document is a legal record of who owns what, “almost exactly” is not good enough.
Exact fractions instead
Instead of storing a share as a decimal, we store it as a fraction: a numerator and a denominator, both integers. One-eighth is 1/8, not 0.125. Adding, multiplying and dividing fractions only ever produces integers, so nothing is lost.
A minimal version looks like this:
type Fraction = { n: bigint; d: bigint };
const gcd = (a: bigint, b: bigint): bigint => (b === 0n ? (a < 0n ? -a : a) : gcd(b, a % b));
function frac(n: bigint, d: bigint = 1n): Fraction {
if (d === 0n) throw new Error('Denominator cannot be zero');
const sign = d < 0n ? -1n : 1n;
const g = gcd(n, d);
return { n: (sign * n) / g, d: (sign * d) / g };
}
const add = (a: Fraction, b: Fraction) => frac(a.n * b.d + b.n * a.d, a.d * b.d);
const mul = (a: Fraction, b: Fraction) => frac(a.n * b.n, a.d * b.d);
Two details matter:
- We use
BigInt, notnumber. After several generations of divisions, denominators get large. Ordinary JavaScript integers lose precision above 2^53;BigIntdoesn’t. - We reduce every result. Dividing by the greatest common divisor keeps numbers small and makes equal fractions compare equal.
With this, a faraiz calculation can check its own work: the heirs’ shares must add up to exactly 1/1. If they don’t, that’s a bug in the rules, not a rounding artefact — and a test catches it.
Only round at the very end
Fractions are for calculating. People still need to read decimals, so we convert to a decimal only when displaying a value, and we round once, at that moment, with a rule chosen for that field — for example, a fixed number of decimal places for land area, and whole taka for fees.
Keeping the exact value until the last step means every screen, every PDF and every Word export shows the same result, because they all round the same exact number in the same way.
Testing it
Exact arithmetic makes tests simple and strict. Instead of “close enough”, we can assert equality:
expect(add(frac(1n, 6n), frac(1n, 3n))).toEqual(frac(1n, 2n));
The calculation logic behind Banglaghora’s tools is covered by several hundred automated tests across the app and its API.
When should you do this?
You don’t need fractions everywhere. Use exact arithmetic when:
- the number has legal or financial meaning,
- values are divided and recombined many times,
- different parts of the system must show identical results, or
- totals must match exactly, like shares of a whole or a ledger balance.
For money specifically, the simpler rule is usually enough: store amounts as integers in the smallest unit (paisa, cents) and never as floating point.
If you’re building software where the numbers have to be right — finance, land, inventory, billing — we’re happy to talk it through.