Topic

Certified approximations

A CertifiedApproximation is one unknown number described two ways: a candidate for display, and an exact enclosure, a rational interval that is guaranteed to contain the true value. Only the enclosure is a promise.

Writing one: the ?

A ? inside a decimal or continued fraction splits it: digits before the ? are certain, digits after it are a guess. 23.456?789 says the true value starts 23.456…23.456\ldots (so lies in [23.456,23.457][23.456, 23.457]) and that the best guess is 23.45678923.456789. A trailing ? with no guess, as in 1.5?, means the value is in [1.5,1.6][1.5, 1.6] and the candidate is 1.51.5. The certain digits truncate toward zero, so for a negative value the enclosure runs the other way: -1.5? is in [−1.6,−1.5][-1.6, -1.5].

23.456?789:candidate 23.456789,enclosure [23.456, 23.457]. \texttt{23.456?789}: \qquad \text{candidate } 23.456789, \qquad \text{enclosure } [23.456,\ 23.457].
import { parseNumber } from "@ratmath/core";

const reading = parseNumber("23.456?789");
reading.candidate.toString();    // "23456789/1000000"
reading.enclosure.toString();    // "2932/125:23457/1000"
parseNumber("3.~7~15?").enclosure.toString();   // "333/106:355/113"
parseNumber("23.456?789[+-12]");  // a narrower enclosure, inside the certified cell

For a continued fraction the certified prefix [3;7,15][3; 7, 15] allows any continuation, so the value lies between 333106\tfrac{333}{106} (stop here) and 355113\tfrac{355}{113} (continue with a 1, the largest step). A bracket after the guess narrows the enclosure, and is rejected if it leaves the certified cell.

Arithmetic

Operations act on both parts: candidates combine as numbers, enclosures combine by interval arithmetic, so the result's enclosure again contains the true result. Two copies of the same reading know they are the same number: subtracting them gives exactly 0, which plain intervals cannot do.

const shifted = reading.add(parseNumber("0.5"));
shifted.enclosure.toString();    // "5989/250:23957/1000"
reading.subtract(reading.copy()).toString();   // "0"

Comparisons that admit doubt

If two enclosures overlap, "is a<ba < b?" has no honest yes-or-no answer. possibleRelations(a, b) returns a bit mask of the relations that are still possible: Relation.LESS, Relation.EQUAL, Relation.GREATER. A single bit means the question is settled.

import { possibleRelations, Relation } from "@ratmath/core";

const m = possibleRelations(parseNumber("2?"), parseNumber("2.5?"));
(m & Relation.LESS) !== 0;       // true: a could be less
(m & Relation.GREATER) !== 0;    // true: or greater, since [2, 3] and [2.5, 2.6] overlap

Cutting an exact value to a budget

boundedDecimalApproximation(q, { fractionalDigits }) returns qq itself when it fits in that many decimal places, and otherwise a certified prefix: 13\tfrac13 to 5 places is 0.33333?. boundedContinuedFractionApproximation(q, { maxTerms }) does the same with continued-fraction terms. Both keep the honest answer ("the value is in this interval") instead of a rounded one.

import { boundedDecimalApproximation, R } from "@ratmath/core";

boundedDecimalApproximation(R`1/3`, { fractionalDigits: 5 }).toString();   // "0.33333?"
boundedDecimalApproximation(R`1/8`, { fractionalDigits: 5 }).toString();   // "1/8", exact

Saving and restoring

toString() is parseable and keeps both parts. JSON.stringify uses the tagged toJSON(), which also keeps where the value came from; read it back with JSON.parse(text, reviveCoreValue).

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