@ratmath/core

Exact numbers for JavaScript

Integers, fractions, intervals and certified approximations that never round. Every value is held as whole numbers (JavaScript BigInt), so 13+16\tfrac13 + \tfrac16 is exactly 12\tfrac12, 0.1+0.20.1 + 0.2 is exactly 0.30.3, and a repeating decimal like 0.142857‾0.\overline{142857} is exactly 17\tfrac17.

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 p/qp/q is stored as the two integers pp and qq, reduced so that gcd⁡(p,q)=1\gcd(p, q) = 1 and q>0q > 0. 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 2\sqrt 2 that no fraction equals. For those Core has rational intervals, closed ranges [a,b][a, b] 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

TypeHoldsRead more
Integera whole number of any sizeIntegers
Rationala fraction in lowest terms; repeating decimals; continued fractionsRationals, Continued fractions
RationalIntervala closed range [a,b][a, b] with rational endsIntervals
RationalIntervalSeta finite union of intervals, open or closed ends, possibly unboundedInterval sets
CertifiedApproximationa candidate value plus an exact enclosureCertified approximations
Fraction, FractionIntervalnumerator and denominator exactly as written (2/4 stays 2/4)Fractions and Stern–Brocot
Regiona set described by chains such as 2n:x:!2n+1Chains and regions
BaseSystem, NumeralSystemdigit alphabets: any base, balanced and negative basesBases

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 2\sqrt 2 exactly (@ratmath/algebraic), or do linear algebra (@ratmath/linalg). Those libraries build on Core's values.

Pages

Try itEdit the code and run it with @ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs