Continued fractions
Every rational has a finite continued fraction, and its prefixes, the convergents, are the best fractions with small denominators. Core computes them exactly, and writes them as 3.~7~16.
Euclid's algorithm, written down
Take the integer part, flip what is left over, and repeat:
RatMath writes as 3.~7~16: the integer part, a dot, then the remaining terms each after a ~.
The coefficients are the quotients of Euclid's algorithm on and , so the expansion of ends after about steps. The last coefficient of the canonical form is greater than 1, because ; the option { long: true } gives the other form.
import { Rational, parseContinuedFraction } from "@ratmath/core";
const v = new Rational(333, 106);
v.toContinuedFraction(); // [3n, 7n, 15n]
v.toContinuedFractionString(); // "3.~7~15"
new Rational(4, 3).toContinuedFraction({ long: true }); // [1n, 2n, 1n]
Rational.fromContinuedFraction([3n, 7n, 15n, 1n]).toString(); // "355/113"
parseContinuedFraction("~-3.~1~3").toString(); // "-9/4" (a negative first term)
Negative values: - or ~-
A minus sign in front means negation, as everywhere else: -3.~1~3 is , the negative of , so . To make the first term itself negative, put a ~ before the sign: ~-3.~1~3 is
Only the first term can be negative; the later terms are positive. Every parser (R, parseNumber, parseContinuedFraction, Rational.fromContinuedFractionString, NumeralSystem.parse) reads both forms this way, as RiX does. The canonical expansion of a negative rational has a negative first term, so toContinuedFractionString() writes it with the ~, and the output reads back as the same value.
parseContinuedFraction("-3.~1~3").toString(); // "-15/4": -(3.~1~3)
parseContinuedFraction("~-3.~1~3").toString(); // "-9/4": first term -3
new Rational(-9, 4).toContinuedFraction(); // [-3n, 1n, 3n]
new Rational(-9, 4).toContinuedFractionString(); // "~-3.~1~3"
new Rational(-15, 4).toContinuedFractionString(); // "~-4.~4": -15/4 = -4 + 1/4
Convergents
Cutting the expansion after gives the convergent . They obey the recurrence
starting from and . Neighbouring convergents satisfy , so they alternate around the value and close in on it:
For an irrational the first inequality is strict; for a rational it can be an equality, as with .
A decimal like 3.14159265358979 is itself an exact rational, so its expansion is finite too; its first terms agree with those of () for as long as the digits do.
Best approximations
Two different questions have nearly the same answer:
bestApproximation(N): the fraction with denominator at most that is closest to . It may be a semiconvergent , an intermediate fraction between two convergents.bestConvergent(N): the last convergent with denominator at most . Convergents are exactly the best approximations "of the second kind", the ones minimizing .
const pi = new Rational(355, 113);
pi.bestApproximation(100).toString(); // "311/99"
pi.bestConvergent(100).toString(); // "22/7"
pi.convergents().map(String); // ["3", "22/7", "355/113"]
new Rational(4, 3).convergents({ intermediates: true }).map(String); // ["1", "2", "3/2", "4/3"]
With intermediates: true, convergents() also lists the semiconvergents between principal convergents. These are the fractions met on the way down the Stern–Brocot tree: a coefficient is a run of steps in one direction.
Prefixes and limits
Options maxTerms (and maxCount for convergents) cap the work, default Rational.DEFAULT_CF_LIMIT. A capped string ends in ~... so it cannot be mistaken for the exact value; the exact parser rejects it. To mark a prefix as a certified approximation instead, write 3.~7~15?: the value lies between the two fractions the prefix allows (see Certified approximations).
@ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.