Fractions and the Stern–Brocot tree
Fraction keeps the numerator and denominator you wrote: stays . That matters for mediants, Farey sequences and the Stern–Brocot tree, where the components, not just the value, carry the structure.
Representation, not just value
import { Fraction, Rational } from "@ratmath/core";
const half = new Fraction(1, 2);
const twoFourths = new Fraction("2/4");
half.equals(twoFourths); // false: different components
half.lessThan(twoFourths); // false, and neither is greater: same value
twoFourths.reduce().toString(); // "1/2"
new Fraction(1, 2).divide(new Fraction(3, 4)).toString(); // "4/6", no cancelling
new Fraction(1, 4).add(new Fraction(2, 4)).toString(); // "3/4" (denominators must match)
With { allowInfinite: true } a nonzero is a signed infinity; and are the outer ends of the tree. is always rejected.
Mediants
The mediant of and adds the parts:
It always lies strictly between the two, and if the pair is Farey neighbours, meaning , then the mediant is the fraction with the smallest denominator between them, and it is a neighbour of each. fareyParents() runs this backwards: the unique neighbours whose mediant is the given fraction.
const left = new Fraction(1, 3), right = new Fraction(1, 2);
const mid = left.mediant(right); // 2/5
Fraction.isFareyTriple(left, mid, right); // true
const p = new Fraction(3, 5).fareyParents(); // { left: 1/2, right: 2/3 }
The Stern–Brocot tree
Start with the ends and and the root . Each fraction's children are the mediants with its nearest ancestors on each side. Every rational appears exactly once, already in lowest terms, so a fraction is named by its path of left (L) and right (R) steps from the root. (The textbook tree starts at between and and holds only positive fractions; this one starts one level up, so it holds the negatives too, mirrored.)
The path is the continued fraction in disguise: for the path from 0 is a run of R's, then L's, R's, and so on, with the last run long. For the path is RLRL; for it is RRRRL. A negative value's path mirrors its absolute value's, with L and R swapped.
const v = new Fraction(3, 5);
v.sternBrocotPath(); // ["R", "L", "R", "L"]
Fraction.fromSternBrocotPath(["R", "L", "R", "L"]).toString(); // "3/5"
v.sternBrocotDepth(); // 4
v.sternBrocotParent().toString(); // "2/3"
v.sternBrocotChildren(); // { left: 4/7, right: 5/8 }
FractionInterval: splitting by mediants
A FractionInterval keeps unreduced ends, so repeated mediant splits keep the unreduced mediants (such as 3/6 below) instead of reducing them. Starting from , the splits are exactly the Stern–Brocot fractions. partitionWithMediants(depth) splits every piece ways. partitionWith(fn) splits at whatever Fractions a function returns.
import { FractionInterval } from "@ratmath/core";
const I = new FractionInterval(new Fraction(1, 3), new Fraction(2, 3));
I.partitionWithMediants(2).map(String); // ["1/3:4/9", "4/9:3/6", "3/6:5/9", "5/9:2/3"]
@ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.