Topic

Intervals

A RationalInterval is a closed range [a,b][a, b] 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 x∈[a,b]x \in [a, b] and y∈[c,d]y \in [c, d], then

x+y∈[a+c, b+d],x−y∈[a−d, b−c], x + y \in [a + c,\ b + d], \qquad x - y \in [a - d,\ b - c], x y∈[min⁡S, max⁡S],S={ac,ad,bc,bd},xy∈[a,b]⋅[1d,1c]  if 0∉[c,d]. x\,y \in [\min S,\ \max S], \quad S = \{ac, ad, bc, bd\}, \qquad \frac{x}{y} \in [a, b] \cdot \left[\tfrac1d, \tfrac1c\right] \ \text{ if } 0 \notin [c, d].

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 x⋅xx \cdot x for x∈[−2,3]x \in [-2, 3] is computed as if the two factors could differ, giving [−6,9][-6, 9], although a square is never negative. pow(n) is the image of one unknown, {xn:x∈[a,b]}\{x^n : x \in [a, b]\}, which is [0,9][0, 9]; mpow(n) is the product of nn 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 [3/4,1/4][3/4, 1/4] equals [1/4,3/4][1/4, 3/4].

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 [1.224,1.235][1.224, 1.235] 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 (a+b)/2(a + b)/2. mediant() is p+rq+s\frac{p + r}{q + s} for ends pq,rs\frac pq, \frac rs, often a simpler fraction. shortestDecimal() is a terminating decimal with the fewest digits in the interval: for [13,23][\tfrac13, \tfrac23] it is 0.40.4. denominatorInterval(d) and randomRational(d) work on the grid of fractions with denominator dd.

Interval sets

RationalIntervalSet is a finite union of disjoint intervals whose ends may be open, closed or unbounded, such as (2,8]∪[10,∞)(2, 8] \cup [10, \infty). 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 (2,8]∪[10,12](2, 8] \cup [10, 12].

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