Intervals
A RationalInterval is a closed range with exact rational ends. Arithmetic on intervals returns an interval that contains every possible result, so a value known only to lie in a range keeps a guarantee through any calculation.
The rules
If and , then
Each result is the smallest interval with that guarantee, because the extremes of a sum or product of independent values are reached at the ends. Dividing by an interval that contains 0 throws: the answer would be unbounded.
import { RationalInterval } from "@ratmath/core";
const a = new RationalInterval(1, 2);
const b = new RationalInterval(3, 4);
a.add(b).toString(); // "4:6"
a.multiply(b).toString(); // "3:8"
a.divide(b).toString(); // "1/4:2/3"
One value or two: pow and mpow
Interval arithmetic treats each operand as its own unknown. So for is computed as if the two factors could differ, giving , although a square is never negative. pow(n) is the image of one unknown, , which is ; mpow(n) is the product of independent factors. This is the dependency problem of interval arithmetic: rewriting a formula so each unknown appears once gives tighter results.
new RationalInterval(-2, 3).pow(2).toString(); // "0:9"
new RationalInterval(-2, 3).mpow(2).toString(); // "-6:9"
Orientation
start and end keep the order you wrote, so 3/4:1/4 prints as written and isAscending is false. low and high are the sorted ends that the arithmetic uses, and equals compares those, so equals .
Comparing and combining
const p = new RationalInterval(1, 3);
const q = new RationalInterval(3, 5);
p.overlaps(q); // true, they share 3
p.intersection(q).toString(); // "3:3"
p.union(q).toString(); // "1:5"
p.containsValue("5/2"); // true
p.contains(new RationalInterval(2, 3)); // true
union returns one interval when the two touch or overlap and null otherwise. For unions with gaps, use interval sets (below).
Writing intervals
A long interval like has several useful spellings. The common-prefix form 1.2[24:35] shares the digits both ends agree on; the midpoint form 1.2295[+-55] gives a centre and a symmetric offset in units of the last digit; the relative form 1.23[+0.5:-0.6] is anchored at a short decimal. All are valid input, so they round-trip.
Points inside
midpoint() is . mediant() is for ends , often a simpler fraction. shortestDecimal() is a terminating decimal with the fewest digits in the interval: for it is . denominatorInterval(d) and randomRational(d) work on the grid of fractions with denominator .
Interval sets
RationalIntervalSet is a finite union of disjoint intervals whose ends may be open, closed or unbounded, such as . The range* functions compute exact images of whole sets, and report the part of the input where an operation is undefined instead of throwing:
import { asRationalIntervalSet, rangeDivide, rangeIntegerPower, R } from "@ratmath/core";
const r = rangeDivide(R`1`, asRationalIntervalSet(R`-1:2`));
r.range.toString(); // "(-inf,-1] U [1/2,inf)"
r.domain.coverage; // "partiallyDefined" (0 was excluded)
rangeIntegerPower(asRationalIntervalSet(R`-2:3`), 2).range.toString(); // "[0,9]"
Sets also come from regions: Region.parse("2:!x:8 || 10:x:_").within(0, 12) is .
@ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.