Topic

Fractions and the Stern–Brocot tree

Fraction keeps the numerator and denominator you wrote: 24\tfrac24 stays 24\tfrac24. 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 a/0a/0 is a signed infinity; −10\tfrac{-1}{0} and 10\tfrac{1}{0} are the outer ends of the tree. 00\tfrac00 is always rejected.

Mediants

The mediant of ab\tfrac ab and cd\tfrac cd adds the parts:

ab⊕cd=a+cb+d,ab<a+cb+d<cd  when ab<cd and b,d>0. \frac ab \oplus \frac cd = \frac{a + c}{b + d}, \qquad \frac ab < \frac{a+c}{b+d} < \frac cd \ \text{ when } \frac ab < \frac cd \text{ and } b, d > 0.

It always lies strictly between the two, and if the pair is Farey neighbours, meaning bc−ad=1bc - ad = 1, 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 −10\tfrac{-1}{0} and 10\tfrac10 and the root 01\tfrac01. 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 11\tfrac11 between 01\tfrac01 and 10\tfrac10 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 x=[a0;a1,…,an]>0x = [a_0; a_1, \ldots, a_n] > 0 the path from 0 is a run of a0+1a_0 + 1 R's, then a1a_1 L's, a2a_2 R's, and so on, with the last run an−1a_n - 1 long. For 35=[0;1,1,2]\tfrac35 = [0; 1, 1, 2] the path is RLRL; for 72=[3;2]\tfrac72 = [3; 2] 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 [01,11][\tfrac01, \tfrac11], the splits are exactly the Stern–Brocot fractions. partitionWithMediants(depth) splits every piece 2depth2^{\text{depth}} 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"]
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