Numbers

Algebraic numbers

A number is algebraic when it is a root of a polynomial with integer coefficients. √2 is a root of x² − 2, the golden ratio of x² − x − 1, ∛2 + 1 of x³ − 3x² + 3x − 3. Everything here is about holding such numbers exactly.

A polynomial and an interval

x² − 2 has two roots, √2 and −√2, so the polynomial alone does not say which number is meant. RatMath adds an interval with rational endpoints that contains exactly one root: √2 is the root of x² − 2 in (1, 2). That pair is the whole number. The interval can be halved again and again, each time keeping the half where the polynomial changes sign, so the number can be printed to any number of digits and every digit printed is certain.

To find such intervals for every real root at once, the library uses a Sturm sequence: p, p′, and then the negated remainders of the Euclidean algorithm. The number of sign changes in the sequence at a point a, minus the number at b, is exactly the number of distinct roots in (a, b]. Splitting an interval until each piece holds one root gives the isolating intervals.

Arithmetic through resultants

If a is a root of p and b a root of q, then a + b is a root of

R(t) = Res_y( p(y), q(t − y) )

the resultant: a polynomial in t whose roots are all the sums a′ + b′ of a root of p with a root of q. Products use q(t / y)·yⁿ instead. The resultant is computed exactly (by evaluating at enough integers and interpolating), then factored, and the result is placed on the one irreducible factor that has it as a root. Which root is decided by shrinking the intervals of a and b until exactly one candidate fits the interval sum. That last factoring step is what keeps degrees small: √2·√8 comes out as the rational 4, not as a root of a degree 4 polynomial.

Minimal polynomials and exact equality

The minimal polynomial of a is the irreducible integer polynomial with a as a root, with the smallest degree. Its degree is the number's degree: 1 for rationals, 2 for √2 or the golden ratio, 3 for ∛2. Because results keep their minimal polynomial, equality is exact: a − b is computed and it is zero only if its minimal polynomial is x. An irrational number can never be mistaken for 0 because of rounding, because nothing is rounded.

Readable forms

format(a) writes a number exactly when a short exact form exists: fractions for rationals, (p + q√d)/r for degree 2, nested radicals such as √(2 + √2)/2 for degree 4 numbers whose square is quadratic (denested to (√6 + √2)/4 when possible). Other numbers print as certified digits ending in "…". toString(n) always gives n digits.

toFixed(k, mode) rounds to k decimal places and every digit it writes is certain. The interval is refined until no rounding boundary lies strictly inside it; an irrational number is never on a boundary, so this always ends, and a rational value is rounded exactly, so a tie such as 5/2 follows the mode ("half-even" by default, or "half-up", "toward-zero", "floor", "ceil"). roundTo(k, mode) gives the same result as a Core Rational.

In code

import { fromRational, sqrt, nthRoot, add, mul, compare, equals, minimalPolynomial, format } from "@ratmath/algebraic";
const two = fromRational(2);
const a = add(sqrt(two), nthRoot(two, 3));
a.poly;                        // its minimal polynomial (degree 6)
a.interval(64);                // [lo, hi] as Core Rationals, width below 2⁻⁶⁴
a.toFixed(10);                 // "2.6741346123", every digit certain
compare(a, fromRational("2.6")); // 1, decided exactly
equals(mul(sqrt(two), sqrt(two)), two); // true

Limits: a resultant of numbers of degrees m and n has degree m·n, so long chains of operations on high-degree numbers get slow. Any result whose minimal polynomial would exceed degree 64 is refused with a RangeError.

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