math.rational
Exact fraction arithmetic, always reduced, for rates and ratios that must not drift.
1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra
Pinned by 17 tests, run in TypeScript, Python and Rust.
What it does
A fraction held as two integers, so 1/3 stays 1/3 and 1/10 + 2/10 is exactly 3/10. Use it for exchange rates, unit conversions and pro-rata factors that are applied more than once: a float rate drifts a little on every step, a rational one never does. Money itself stays in `money.amount` minor units; turn a rational back into an integer with `rationalToInteger` and an explicit rounding mode.
Every result is reduced (numerator and denominator share no factor) and its denominator is positive, so two equal values always have the same fields and compare equal as plain data. Inputs need not be reduced: `{2, -4}` is read as -1/2.
For example
calculateRational(numerator 1, denominator 3, add, numerator 1, denominator 6)→ numerator 1, denominator 2 a third plus a sixth is a halfcalculateRational(numerator 1, denominator 10, add, numerator 2, denominator 10)→ numerator 3, denominator 10 one tenth plus two tenths is exactly three tenths, unlike 0.1 + 0.2calculateRational(numerator 3, denominator 4, subtract, numerator 5, denominator 4)→ numerator -1, denominator 2 subtracting past zero gives a negative numerator
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
export function calculateRational(a: Rational, op: RationalOp, b: Rational): Rational
| a | Rational | left operand; need not be reduced |
| op | RationalOp | add, subtract, multiply or divide |
| b | Rational | right operand; need not be reduced |
| returns | Rational |
The types it declares, generated into your project
/** An exact fraction. Results are always reduced, with a positive denominator. */
export interface Rational {
/** carries the sign */
readonly numerator: number;
/** never zero; positive in every result */
readonly denominator: number;
}
export type RationalOp = "add" | "subtract" | "multiply" | "divide";
Your code names it in one line, in the file that uses it
import { calculateRational } from "#fune/math.rational@^1";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { gcdWide } from "./math_gcd_lcm.ts"; ← from math.gcd-lcm ^1.0.0 · built alongside by fune
import { type RoundingMode, roundDiv } from "./math_round_div.ts"; ← from math.round-div ^1.0.0 · built alongside by fune
import { type Rational, type RationalOp } from "./math_rational_types.ts";
const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
/** Reduce a wide fraction and bring it back into the exact number range. */
function reduce(numerator: bigint, denominator: bigint): Rational {
if (denominator === 0n) {
throw new RangeError("denominator must not be zero");
}
const g = gcdWide(numerator, denominator);
let n = numerator / g;
let d = denominator / g;
if (d < 0n) {
n = -n;
d = -d;
}
if (n > MAX_SAFE || n < -MAX_SAFE || d > MAX_SAFE) {
throw new RangeError("rational overflow: the reduced result exceeds 2^53 - 1");
}
// Number(-0n) is 0, so a zero result never comes back as -0.
return { numerator: Number(n), denominator: Number(d) };
}
/** Build a reduced fraction with a positive denominator. */
export function rational(numerator: number, denominator: number): Rational {
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
throw new TypeError("numerator and denominator must be integers");
}
if (!Number.isSafeInteger(numerator) || !Number.isSafeInteger(denominator)) {
throw new RangeError("rational overflow: numerator and denominator must be within 2^53 - 1");
}
return reduce(BigInt(numerator), BigInt(denominator));
}
function wide(r: Rational): [bigint, bigint] {
const n = rational(r.numerator, r.denominator);
return [BigInt(n.numerator), BigInt(n.denominator)];
}
export function addRational(a: Rational, b: Rational): Rational {
const [an, ad] = wide(a);
const [bn, bd] = wide(b);
return reduce(an * bd + bn * ad, ad * bd);
}
export function subtractRational(a: Rational, b: Rational): Rational {
const [an, ad] = wide(a);
const [bn, bd] = wide(b);
return reduce(an * bd - bn * ad, ad * bd);
}
export function multiplyRational(a: Rational, b: Rational): Rational {
const [an, ad] = wide(a);
const [bn, bd] = wide(b);
return reduce(an * bn, ad * bd);
}
export function divideRational(a: Rational, b: Rational): Rational {
const [an, ad] = wide(a);
const [bn, bd] = wide(b);
if (bn === 0n) {
throw new RangeError("division by zero");
}
return reduce(an * bd, ad * bn);
}
/** -1, 0 or 1. Exact: cross-multiplied wide, never through a float. */
export function compareRational(a: Rational, b: Rational): number {
const [an, ad] = wide(a);
const [bn, bd] = wide(b);
const left = an * bd;
const right = bn * ad;
return left < right ? -1 : left > right ? 1 : 0;
}
/** The nearest integer under an explicit rounding mode. */
export function rationalToInteger(r: Rational, mode: RoundingMode): number {
const n = rational(r.numerator, r.denominator);
return roundDiv(n.numerator, n.denominator, mode);
}
/**
* Add, subtract, multiply or divide two fractions exactly.
*
* The result is always reduced with a positive denominator, so equal values
* have equal fields.
*/
export function calculateRational(a: Rational, op: RationalOp, b: Rational): Rational {
switch (op) {
case "add":
return addRational(a, b);
case "subtract":
return subtractRational(a, b);
case "multiply":
return multiplyRational(a, b);
case "divide":
return divideRational(a, b);
default:
throw new RangeError(`unknown operation "${op}"`);
}
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add math.rational
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./math.rational-1.0.0-typescript.fune, or fetch it from a terminal with fune pull math.rational@1.0.0:typescript.
The whole function, every language, is one file too: math.rational-1.0.0.fune, 20,595 bytes, sha256 525db2981a46569bdeb824d6acadf1eaee1c94a6b4dd0feb5893496901d12f79. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
// fune: before math.rational
after — your function gets the result and the arguments, and returns the final result.
// fune: after math.rational
replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.
// fune: replace math.gcd-lcm in math.rational
// fune: replace math.round-div in math.rational
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show math.rational --steps.
// fune: step math.rational after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| a third plus a sixth is a half | numerator 1, denominator 3, add, numerator 1, denominator 6 | → | numerator 1, denominator 2 |
| one tenth plus two tenths is exactly three tenths, unlike 0.1 + 0.2 | numerator 1, denominator 10, add, numerator 2, denominator 10 | → | numerator 3, denominator 10 |
| subtracting past zero gives a negative numerator | numerator 3, denominator 4, subtract, numerator 5, denominator 4 | → | numerator -1, denominator 2 |
| equal values subtract to zero over one | numerator 1, denominator 2, subtract, numerator 2, denominator 4 | → | numerator 0, denominator 1 |
| multiplying reduces the result | numerator 2, denominator 3, multiply, numerator 9, denominator 4 | → | numerator 3, denominator 2 |
| dividing by a quarter doubles a half into a whole number | numerator 1, denominator 2, divide, numerator 1, denominator 4 | → | numerator 2, denominator 1 |
| dividing by a negative puts the sign on the numerator | numerator 1, denominator 3, divide, numerator -2, denominator 3 | → | numerator -1, denominator 2 |
| unreduced inputs with a negative denominator are normalised | numerator 2, denominator -4, add, numerator 0, denominator 5 | → | numerator -1, denominator 2 |
| negative over negative is positive | numerator -3, denominator -9, multiply, numerator 1, denominator 1 | → | numerator 1, denominator 3 |
| unreduced products past 2^53 still reduce exactly | numerator 9,007,199,254,740,991, denominator 2, multiply, numerator 2, denominator 9,007,199,254,740,991 | → | numerator 1, denominator 1 |
Show the other 7 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| a common denominator past 2^53 before reducing | numerator 1, denominator 4,503,599,627,370,496, add, numerator 1, denominator 4,503,599,627,370,496 | → | numerator 1, denominator 2,251,799,813,685,248 |
| an exchange rate and its inverse multiply to exactly one | numerator 11,734, denominator 10,000, multiply, numerator 10,000, denominator 11,734 | → | numerator 1, denominator 1 |
| a numerator past 2^53 - 1 is an overflow, not a rounded number | numerator 9,007,199,254,740,991, denominator 1, add, numerator 1, denominator 1 | → | error: rational overflow |
| a denominator past 2^53 - 1 is an overflow | numerator 1, denominator 9,007,199,254,740,991, multiply, numerator 1, denominator 2 | → | error: rational overflow |
| dividing by zero is an error | numerator 1, denominator 2, divide, numerator 0, denominator 5 | → | error: division by zero |
| a zero denominator is an error | numerator 1, denominator 0, add, numerator 1, denominator 2 | → | error: denominator must not be zero |
| an unknown operation is an error | numerator 1, denominator 2, power, numerator 1, denominator 2 | → | error: unknown operation |
More from the author
The vectored entry is `calculateRational(a, op, b)`. The module also exports `rational(n, d)` (build and reduce), `addRational`, `subtractRational`, `multiplyRational`, `divideRational`, `compareRational(a, b)` (-1, 0 or 1) and `rationalToInteger(r, mode)` (the `math.round-div` modes), which are the same arithmetic under separate names.
**Limits.** A numerator or denominator must be an integer within ±(2^53 - 1), the range where a JavaScript number is exact, in inputs and in results. Intermediate products are computed wide (TypeScript `bigint`, Python `int`, Rust `i128`) and reduced before the check, so `(2^53-1)/2 × 2/(2^53-1)` is 1 even though the unreduced product is far past the limit. A result that is still too large after reducing is an error ("rational overflow"), never a silently wrong fraction.
Level 1 rather than the catalogue's 0, because it builds on `math.gcd-lcm` and `math.round-div` instead of carrying private copies of them.
Files
| Path | Bytes |
|---|---|
| README.md | 1,610 |
| impl/python.py | 3,128 |
| impl/rust.rs | 3,979 |
| impl/typescript.ts | 3,418 |
| vectors.json | 5,179 |