Exact numbers for JavaScript
Integers, fractions, intervals and certified approximations that never round. Every value is held as whole numbers (JavaScript BigInt), so is exactly , is exactly , and a repeating decimal like is exactly .
The box above reads RatMath's number notation through the R template. Try 1:2 * 2 (an interval), 0.#3 * 3 (a repeating decimal times 3 is the integer 1), or 3.~7~15~1 (a continued fraction, which is 355/113).
Why exact
A JavaScript number is a binary fraction with 53 significant bits. Most decimal fractions have no such form, so 0.1 + 0.2 gives 0.30000000000000004, and every further step can add another rounding. Core avoids the question: a rational number is stored as the two integers and , reduced so that and . Sums, products and quotients of rationals are rationals, so arithmetic never needs to round.
Some quantities are not single rationals: a measurement known to within a tolerance, or a value like that no fraction equals. For those Core has rational intervals, closed ranges with exact ends, and certified approximations, a displayed guess plus an exact interval that is guaranteed to contain the true value. Interval arithmetic returns a range that contains every possible result, so a guarantee survives every step.
Install
npm install @ratmath/core
An ES module with no runtime dependencies, for Node.js 22 or newer, Bun, Deno and browsers. TypeScript declarations are included.
A first program
import { R, Rational, parseDecimal, range, sum } from "@ratmath/core";
R`1/3 + 1/6`; // Rational 1/2
R`0.1 + 0.2`; // Rational 3/10, not 0.30000000000000004
R`1:2 * 3`; // RationalInterval 3:6
R`0.#142857 * 7`; // Integer 1
const price = parseDecimal("19.99");
price.multiply(parseDecimal("0.0825")).toString(); // "65967/40000", the exact tax
new Rational(1, 7).toRepeatingDecimal(); // "0.#142857"
new Rational(355, 113).toContinuedFractionString(); // "3.~7~16"
sum(1, 10, (k) => R`1/${k}`).toString(); // "7381/2520"
The types
| Type | Holds | Read more |
|---|---|---|
| Integer | a whole number of any size | Integers |
| Rational | a fraction in lowest terms; repeating decimals; continued fractions | Rationals, Continued fractions |
| RationalInterval | a closed range with rational ends | Intervals |
| RationalIntervalSet | a finite union of intervals, open or closed ends, possibly unbounded | Interval sets |
| CertifiedApproximation | a candidate value plus an exact enclosure | Certified approximations |
| Fraction, FractionInterval | numerator and denominator exactly as written (2/4 stays 2/4) | Fractions and Stern–Brocot |
| Region | a set described by chains such as 2n:x:!2n+1 | Chains and regions |
| BaseSystem, NumeralSystem | digit alphabets: any base, balanced and negative bases | Bases |
All values are immutable: every operation returns a new value. Mixed arithmetic promotes upward (Integer to Rational to RationalInterval), and an exact division of integers stays an Integer; see Types and promotion.
What Core does not do
Core is the exact-number layer. It does not evaluate transcendental functions such as sin or exp (that is @ratmath/analytic), hold algebraic numbers like exactly (@ratmath/algebraic), or do linear algebra (@ratmath/linalg). Those libraries build on Core's values.
Pages
2:!x:8
BasesBinary, hex, balanced ternary
Stern–BrocotMediants and the tree of fractions
@ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.