Certified answers
A certified answer is a RationalInterval: two fractions with the true value between them. This page says how to read one, how to make it narrower, and what happens when a decision depends on a value that sits on the boundary.
Reading an enclosure
import { normal, show, toNumber, compare } from "@ratmath/stats";
const p = normal().sf("1.96");
p.low; p.high; // the two rationals; the true value is between them
show(p, 20); // "0.02499789514822043414": the digits both ends share
show(p, 40); // "0.0249978951482204341365842690408…": shared digits, then "…"
toNumber(p); // 0.024997895148220435: the midpoint, as a JS number
show(value, digits) prints a fraction rounded to that many places. For an enclosure it prints the digits on which both ends agree, and marks with … the place where they stop agreeing. Digits it prints are digits of the true value's enclosure, not of a floating-point estimate.
Precision on request
const narrow = normal().sf("1.96", { bits: 300 });
show(narrow, 80);
// "0.02499789514822043413658426904083719002249977906188339108571710412881842600366650"
bits is a target for the width: about . The work grows with the precision, and there is no limit built in.
Comparisons that do not guess
compare(normal().sf("1.96"), "0.025"); // "below": certainly less than 0.025
compare(normal().sf("1.95"), "0.025"); // "above"
// a p-value computed to only 30 bits, right at the boundary
const z = normal().quantile("0.975", { bits: 20 }); // an enclosure of 1.95996…
compare(normal().sf(z, { bits: 30 }), "0.025"); // "too close to call"
compare(value, level) answers "below" when the whole enclosure is below the level, "above" when it is all above, and "too close to call" when the level is inside. For an exact value it can also answer "equal". A test result's decision(alpha) uses the same rule and returns "reject", "keep" or "too close to call"; reject(alpha) returns true or false and refuses in the third case, telling you to ask for more precision.
In floating point a p-value of 0.05000000000000001 and one of 0.04999999999999999 may be the same number with different rounding. Here the question "is ?" has an honest third answer, and more bits will usually resolve it.
How a quantile is enclosed
For an increasing distribution function and a level , the quantile is the with . The solver keeps two rationals with
where each inequality has been certified: the enclosure of lies entirely below . Since is increasing, the quantile is between and . Secant steps (with bisection when progress stalls) move the ends together until is about times the size of the answer. To decide a sign near the crossing the enclosure of must be narrower than its distance from , so the working precision rises as the bracket closes.
The floating-point guess only sets where the search starts. A bad guess costs time and cannot cost correctness.
Where exact answers come back
Certified does not mean always an interval. exponential(2).quantile("1/2") is an enclosure of , but uniform(0, 4).quantile("1/4") is the fraction 1. Standard deviations are exact when the variance is a perfect square. Discrete quantiles are whole numbers. The library returns a fraction whenever it has one.
@ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.