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 (so lies in ) and that the best guess is . A trailing ? with no guess, as in 1.5?, means the value is in and the candidate is . The certain digits truncate toward zero, so for a negative value the enclosure runs the other way: -1.5? is in .
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 allows any continuation, so the value lies between (stop here) and (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 ?" 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 itself when it fits in that many decimal places, and otherwise a certified prefix: 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).
@ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.