interval design

interval computes with sets of real numbers instead of single approximations. This page states the mathematical contract, derives the formulas the code uses, explains the choices behind them, and lists what the package does not do. The API reference documents each function; the tutorial shows them in use.

Design goal

A floating-point computation returns a number near the true result, with an error that the caller has to estimate separately. interval returns an interval that provably contains the true result, at any working precision, so that the error bound is part of the value. Three requirements follow:

  1. Inclusion before tightness. Every operation returns a superset of the exact image of its operands. A wider result is acceptable; a result that misses a possible value is a bug.
  2. Explicit semantics. Precision, target format, flags and decorations are values passed in and returned, never process-global state (no hardware rounding-mode switching).
  3. Standard vocabulary. Sets, relations and decorations follow IEEE 1788-201511 IEEE Std 1788-2015, IEEE Standard for Interval Arithmetic. The package follows its set-based flavor. , so results can be checked against its test corpus (see conformance).

Mathematical background

Intervals and the inclusion property

An interval is a set x=[x‾,x‾]={ξ∈R:x‾≤ξ≤x‾}\boldsymbol{x} = [\underline{x}, \overline{x}] = \{\xi \in \mathbb{R} : \underline{x} \le \xi \le \overline{x}\} with x‾∈R∪{−∞}\underline{x} \in \mathbb{R} \cup \{-\infty\}, x‾∈R∪{+∞}\overline{x} \in \mathbb{R} \cup \{+\infty\} and x‾≤x‾\underline{x} \le \overline{x}, or the empty set ∅\emptyset. Following the set-based model of IEEE 1788, infinite endpoints only say that the set is unbounded; the elements are always real. The whole line (−∞,+∞)(-\infty, +\infty) is called Entire.

For a real function ff with domain Df⊆RnD_f \subseteq \mathbb{R}^n, the range over a box is

f(x)={f(ξ):ξ∈x∩Df},x=x1×⋯×xn,f(\boldsymbol{x}) = \{ f(\xi) : \xi \in \boldsymbol{x} \cap D_f \}, \qquad \boldsymbol{x} = \boldsymbol{x}_1 \times \cdots \times \boldsymbol{x}_n ,

and its hull hull⁡f(x)\operatorname{hull} f(\boldsymbol{x}) is the smallest interval containing it. An interval extension of ff is a map FF with f(x)⊆F(x)f(\boldsymbol{x}) \subseteq F(\boldsymbol{x}) for every box. Points outside DfD_f are ignored rather than reported as errors: [−1,4]=[0,2]\sqrt{[-1, 4]} = [0, 2] and [−2,−1]=∅\sqrt{[-2, -1]} = \emptyset. Whether a domain violation occurred is reported separately, by decorations.

Every public operation of Interval is an interval extension; no exception is listed at present (see Known limitations). Composition then gives the result that justifies the package:

Fundamental theorem of interval arithmetic.22 R. E. Moore, Interval Analysis, Prentice-Hall, 1966; and Moore, Kearfott, Cloud, Introduction to Interval Analysis, SIAM, 2009, Thm. 5.1. If an expression is evaluated with an interval extension for each operation, the result contains the value of the expression at every point of the input box where the expression is defined.

Proof. By induction on the expression. A variable ξi\xi_i evaluates to xi∋ξi\boldsymbol{x}_i \ni \xi_i. For e=g(e1,…,ek)e = g(e_1, \dots, e_k), let ξ\xi be a point where ee is defined; then each eje_j is defined at ξ\xi and, by the induction hypothesis, yj=ej(ξ)∈Ejy_j = e_j(\xi) \in E_j, the interval value of eje_j. Since gg is defined at (y1,…,yk)(y_1, \dots, y_k) and GG extends gg,

e(ξ)=g(y1,…,yk)∈g(E1×⋯×Ek)yj∈Ej⊆G(E1,…,Ek)=E(x)G is an interval extension.\begin{aligned} e(\xi) = g(y_1, \dots, y_k) &\in g(E_1 \times \cdots \times E_k) && y_j \in E_j \\ &\subseteq G(E_1, \dots, E_k) = E(\boldsymbol{x}) && G \text{ is an interval extension.} \end{aligned}

□\square The full argument, with the lemmas below, is in the attachment:

Inclusion proofs for interval

Outward rounding

Endpoints are Binary numbers with pp significant bits, so exact endpoint values must be rounded. Write FpF_p for the pp-bit binary numbers and

RD⁡p(a)=max⁡{f∈Fp:f≤a},RU⁡p(a)=min⁡{f∈Fp:f≥a}.\operatorname{RD}_p(a) = \max\{ f \in F_p : f \le a \}, \qquad \operatorname{RU}_p(a) = \min\{ f \in F_p : f \ge a \}.

Three properties follow directly from these definitions:

(i)RD⁡p(a)≤a≤RU⁡p(a),(ii)a≤b  ⟹  RD⁡p(a)≤RD⁡p(b) and RU⁡p(a)≤RU⁡p(b),(iii)min⁡s∈SRD⁡p(s)=RD⁡p(min⁡S),max⁡s∈SRU⁡p(s)=RU⁡p(max⁡S).\begin{aligned} &\text{(i)}\quad \operatorname{RD}_p(a) \le a \le \operatorname{RU}_p(a), \\ &\text{(ii)}\quad a \le b \implies \operatorname{RD}_p(a) \le \operatorname{RD}_p(b) \text{ and } \operatorname{RU}_p(a) \le \operatorname{RU}_p(b), \\ &\text{(iii)}\quad \min_{s \in S} \operatorname{RD}_p(s) = \operatorname{RD}_p(\min S), \quad \max_{s \in S} \operatorname{RU}_p(s) = \operatorname{RU}_p(\max S). \end{aligned}

(ii) holds because the set maximized for RD⁡p(b)\operatorname{RD}_p(b) contains the one for RD⁡p(a)\operatorname{RD}_p(a); (iii) follows from (ii), since the minimum over SS is attained at min⁡S\min S. Hence, if L≤inf⁡AL \le \inf A and U≥sup⁡AU \ge \sup A for a set AA,

A⊆[L,U]⊆[RD⁡p(L),RU⁡p(U)].A \subseteq [L, U] \subseteq [\operatorname{RD}_p(L), \operatorname{RU}_p(U)].

This is the only way rounding enters the package: each operation computes a lower bound LL and an upper bound UU of the exact range and stores RD⁡p(L)\operatorname{RD}_p(L) and RU⁡p(U)\operatorname{RU}_p(U). Property (iii) means it does not matter whether candidates are rounded before or after taking the minimum; the code rounds the extremal candidate once (quantize_interval).

The width added by rounding is at most one unit in the last place per endpoint. For 2e≤∣a∣<2e+12^{e} \le |a| < 2^{e+1} the pp-bit numbers near aa are spaced ulp⁡p(a)=2e−p+1\operatorname{ulp}_p(a) = 2^{e-p+1} apart, so RU⁡p(a)−a<ulp⁡p(a)≤21−p∣a∣\operatorname{RU}_p(a) - a < \operatorname{ulp}_p(a) \le 2^{1-p}|a|, and likewise for RD⁡p\operatorname{RD}_p. For a sum with exact range [Sℓ,Su][S_\ell, S_u]:

RU⁡p(Su)−RD⁡p(Sℓ)≤(Su+ulp⁡p(Su))−(Sℓ−ulp⁡p(Sℓ))≤w(x)+w(y)+21−p(∣Su∣+∣Sℓ∣),\begin{aligned} \operatorname{RU}_p(S_u) - \operatorname{RD}_p(S_\ell) &\le (S_u + \operatorname{ulp}_p(S_u)) - (S_\ell - \operatorname{ulp}_p(S_\ell)) \\ &\le w(\boldsymbol{x}) + w(\boldsymbol{y}) + 2^{1-p}\bigl(|S_u| + |S_\ell|\bigr), \end{aligned}

using Su−Sℓ=w(x)+w(y)S_u - S_\ell = w(\boldsymbol{x}) + w(\boldsymbol{y}). Widths therefore grow additively through a computation, plus a relative 21−p2^{1-p} per rounding. The relative bound needs aa inside the normal range of Binary, 2emin⁡≤∣a∣<2emax⁡+12^{e_{\min}} \le |a| < 2^{e_{\max}+1} with emin⁡=1−emax⁡=−(230−2)e_{\min} = 1 - e_{\max} = -(2^{30} - 2) and emax⁡=230−1e_{\max} = 2^{30} - 1. Below 2emin⁡2^{e_{\min}} the directed kernels round on a subnormal grid, and the error is only bounded absolutely, by 2emin⁡−p+12^{e_{\min} - p + 1}; above the range an upper endpoint becomes +∞+\infty. Both happen only for magnitudes near 2±2302^{\pm 2^{30}}.

Endpoint formulas

Interval stores the two endpoints (see Endpoints, not midpoint and radius), so each operation is a formula in the endpoints.

Sum and difference. ξ+η\xi + \eta is increasing in both arguments and ξ−η\xi - \eta is increasing in ξ\xi and decreasing in η\eta, so the extremes over the box are at the corners that make each argument extreme in the right direction:

x+y=[x‾+y‾, x‾+y‾],x−y=[x‾−y‾, x‾−y‾].\begin{aligned} \boldsymbol{x} + \boldsymbol{y} &= [\underline{x} + \underline{y},\ \overline{x} + \overline{y}], & \boldsymbol{x} - \boldsymbol{y} &= [\underline{x} - \overline{y},\ \overline{x} - \underline{y}]. \end{aligned}

Product. For fixed η\eta, ξ↦ξη\xi \mapsto \xi\eta is affine, so its extrema over x\boldsymbol{x} lie at x‾\underline{x} or x‾\overline{x}; applying the same argument to η\eta gives

xy=[min⁡S,max⁡S],S={x‾ y‾, x‾ y‾, x‾ y‾, x‾ y‾}.\boldsymbol{x}\boldsymbol{y} = [\min S, \max S], \qquad S = \{\underline{x}\,\underline{y},\ \underline{x}\,\overline{y},\ \overline{x}\,\underline{y},\ \overline{x}\,\overline{y}\}.

The sign pattern decides which elements of SS can be extremal. If x‾,y‾≥0\underline{x}, \underline{y} \ge 0 the product is increasing in both arguments, so the range is [x‾ y‾,x‾ y‾][\underline{x}\,\underline{y}, \overline{x}\,\overline{y}]; the other single-sign cases follow from ξη=(−ξ)(−η)=−((−ξ)η)\xi\eta = (-\xi)(-\eta) = -((-\xi)\eta), which gives the table used by multiplication_bounds:

x\boldsymbol{x}y\boldsymbol{y}lowerupper
≥0\ge 0≥0\ge 0x‾ y‾\underline{x}\,\underline{y}x‾ y‾\overline{x}\,\overline{y}
≤0\le 0≤0\le 0x‾ y‾\overline{x}\,\overline{y}x‾ y‾\underline{x}\,\underline{y}
≥0\ge 0≤0\le 0x‾ y‾\overline{x}\,\underline{y}x‾ y‾\underline{x}\,\overline{y}
≤0\le 0≥0\ge 0x‾ y‾\underline{x}\,\overline{y}x‾ y‾\overline{x}\,\underline{y}
other (one of them has 00 in its interior)min⁡(x‾ y‾,x‾ y‾)\min(\underline{x}\,\overline{y}, \overline{x}\,\underline{y})max⁡(x‾ y‾,x‾ y‾)\max(\underline{x}\,\underline{y}, \overline{x}\,\overline{y})

The first four rows cover the cases where both operands have constant sign (an operand with a zero endpoint, such as [0,2][0, 2], has constant sign; when two rows apply they give the same products). The last row covers the five remaining cases, in which at least one operand straddles 0. Say x‾<0<x‾\underline{x} < 0 < \overline{x}. The minimum of SS is ≤0\le 0, because x‾ η\underline{x}\,\eta and x‾ η\overline{x}\,\eta have opposite signs for any η\eta. If it were attained at x‾ y‾<0\underline{x}\,\underline{y} < 0, then y‾>0\underline{y} > 0, hence y‾≥y‾\overline{y} \ge \underline{y} gives x‾ y‾≤x‾ y‾\underline{x}\,\overline{y} \le \underline{x}\,\underline{y}; if at x‾ y‾<0\overline{x}\,\overline{y} < 0, then y‾<0\overline{y} < 0 and x‾ y‾≤x‾ y‾\overline{x}\,\underline{y} \le \overline{x}\,\overline{y}. Either way the minimum is also attained at x‾ y‾\underline{x}\,\overline{y} or x‾ y‾\overline{x}\,\underline{y} (and a minimum of 0 at x‾ y‾\underline{x}\,\underline{y} means y‾=0\underline{y} = 0, so x‾ y‾≤0\underline{x}\,\overline{y} \le 0 as well). The maximum and the case where y\boldsymbol{y} straddles 0 are symmetric. So two products suffice for the four single-sign rows and four for the rest.

For unbounded intervals all four products are formed with 0⋅∞:=00 \cdot \infty := 0. This is the right limit for the set-based model: if x‾=0\underline{x} = 0 and y‾=+∞\overline{y} = +\infty, the products ξη\xi\eta with ξ↓0\xi \downarrow 0 and η\eta fixed tend to 0, while the unbounded growth is already represented by the other corners (x‾ y‾=±∞\overline{x}\,\overline{y} = \pm\infty when x‾≠0\overline{x} \ne 0).

Division. IEEE 1788 defines x/y=hull⁡{ξ/η:ξ∈x,η∈y,η≠0}\boldsymbol{x}/\boldsymbol{y} = \operatorname{hull}\{\xi/\eta : \xi \in \boldsymbol{x}, \eta \in \boldsymbol{y}, \eta \ne 0\}. When 0∉y0 \notin \boldsymbol{y}, 1/η1/\eta is continuous and decreasing on y\boldsymbol{y}, so 1/y=[1/y‾,1/y‾]1/\boldsymbol{y} = [1/\overline{y}, 1/\underline{y}] and x/y=x⋅(1/y)\boldsymbol{x}/\boldsymbol{y} = \boldsymbol{x} \cdot (1/\boldsymbol{y}); the code evaluates the selected endpoint quotients directly with directed division instead of rounding twice. When 0∈y0 \in \boldsymbol{y}:

  • y={0}\boldsymbol{y} = \{0\}: no admissible η\eta, the result is ∅\emptyset.
  • y‾<0<y‾\underline{y} < 0 < \overline{y}, x≠{0}\boldsymbol{x} \ne \{0\}: η\eta approaches 0 from both sides, so ξ/η\xi/\eta is unbounded in both directions: Entire.
  • y=[0,y‾]\boldsymbol{y} = [0, \overline{y}] with y‾>0\overline{y} > 0 and x‾≥0\underline{x} \ge 0, x≠{0}\boldsymbol{x} \ne \{0\}: as η↓0\eta \downarrow 0 with ξ=x‾>0\xi = \overline{x} > 0, ξ/η→+∞\xi/\eta \to +\infty, and the smallest quotient is x‾/y‾\underline{x}/\overline{y} (0 when y‾=+∞\overline{y} = +\infty), so the result is [x‾/y‾,+∞)[\underline{x}/\overline{y}, +\infty). The cases x‾≤0\overline{x} \le 0 and y=[y‾,0]\boldsymbol{y} = [\underline{y}, 0] are mirror images, obtained from ξ/η=(−ξ)/(−η)=−((−ξ)/η)\xi/\eta = (-\xi)/(-\eta) = -((-\xi)/\eta). If x\boldsymbol{x} straddles 0, both signs are unbounded: Entire.
  • x={0}\boldsymbol{x} = \{0\} and y≠{0}\boldsymbol{y} \ne \{0\}: every admissible quotient is 0.
  • An Empty operand gives Empty.

From midpoint–radius to endpoints

Users often know a value as c±rc \pm r. Interval::from_midpoint_radius(c, r, precision=p) is the hull in the format of pp bits of the interval [c−r,c+r][c - r, c + r] (IEEE 1788-2015 12.12.7, midRadToInterval): it stores

[RD⁡p(c−r), RU⁡p(c+r)],[\operatorname{RD}_p(c - r),\ \operatorname{RU}_p(c + r)],

each end the exact sum rounded once in its direction (sub_ctx toward negative and add_ctx toward positive of binary, whose operands are exact whatever their precision). The interval contains [c−r,c+r][c - r, c + r] because a directed rounding never crosses the value, and it is the tightest interval of the format that does. When c−rc - r and c+rc + r are numbers of the format nothing is rounded. Before 0.12 the center was rounded to nearest, its rounding error was added to the radius on both sides and the ends c~±R\tilde c \pm R were kept exact with more than pp bits, which made the type hold bounds that were not numbers of its format; the hull above replaced it (the changelog states the largest difference). with_precision rounds the endpoints outward directly, which encloses the input at every precision and is the tightest representable enclosure, so it has no rounding-mode argument. The midpoint-radius view of the interval is the pair that IEEE 1788-2015 12.12.8 defines: to_midpoint_radius_string prints midpoint() and radius(), numbers of the format of pp bits. mid is the exact center (x‾+x‾)/2(\underline{x} + \overline{x})/2 rounded to nearest (ties to even), and rad is the smallest number of the format with the interval inside [m−r,m+r][m - r, m + r] for that rounded mm: radius measures from midpoint, it is the larger of m−x‾m - \underline{x} and x‾−m\overline{x} - m, rounded up, so the printed pair encloses the interval (Interval::radius). The half-width around the exact center would be too small when the center is not representable (it was returned as radius before 0.12, issue 477); and the exact center of bounds that are billions of places apart is a number of billions of bits, which a text view cannot print: before 0.12 the text printed the exact center and half-width, with a stand-in beyond a gap of 65,536 places that was not the center.

The dependency problem

The fundamental theorem treats every occurrence of a variable as an independent point. For x=[1,2]\boldsymbol{x} = [1, 2],

x−x=[1−2, 2−1]=[−1,1]⊋{0}={ξ−ξ:ξ∈x}.\boldsymbol{x} - \boldsymbol{x} = [1 - 2,\ 2 - 1] = [-1, 1] \supsetneq \{0\} = \{\xi - \xi : \xi \in \boldsymbol{x}\}.

The result is correct (it contains 0) but not tight: interval subtraction is the extension of (ξ,η)↦ξ−η(\xi, \eta) \mapsto \xi - \eta, and the box x×x\boldsymbol{x} \times \boldsymbol{x} contains (1,2)(1, 2) and (2,1)(2, 1). In general only f(x)⊆F(x)f(\boldsymbol{x}) \subseteq F(\boldsymbol{x}) holds. A second theorem of Moore’s gives equality, in exact arithmetic and for continuous operations, when each variable occurs at most once in the expression; the condition is sufficient, not necessary (x⋅x\boldsymbol{x} \cdot \boldsymbol{x} is exact for x=[1,2]\boldsymbol{x} = [1, 2]). The same effect explains xx⊋x2\boldsymbol{x}\boldsymbol{x} \supsetneq \boldsymbol{x}^2 for 0∈int⁡x0 \in \operatorname{int}\boldsymbol{x} ([−1,2]⋅[−1,2]=[−2,4][-1, 2]\cdot[-1, 2] = [-2, 4] but [−1,2]2=[0,4][-1, 2]^2 = [0, 4]) and subdistributivity, x(y+z)⊆xy+xz\boldsymbol{x}(\boldsymbol{y} + \boldsymbol{z}) \subseteq \boldsymbol{x}\boldsymbol{y} + \boldsymbol{x}\boldsymbol{z}. For this reason the package provides single-occurrence operations — sqr, pown, fma, hypot, cancel_minus and the elementary functions — whose results are hulls of the true range (up to rounding) rather than products of independent factors. Interval values also do not form a group under addition: cancel_minus is the operation that undoes an addition, since (x+y)−y≠x(\boldsymbol{x} + \boldsymbol{y}) - \boldsymbol{y} \ne \boldsymbol{x} in general.

Elementary functions: monotonicity, critical points and poles

For a continuous function the range over an interval is an interval, and its endpoints are attained at the endpoints of x\boldsymbol{x} or at critical points inside it. The package uses three patterns.

  • Monotone functions (exp, exp2, exp10, expm1, ln, log2, log10, log1p, sqrt, sinh, tanh, asinh, acosh, atanh, asin, atan, and the decreasing acos): the range over x∩Df\boldsymbol{x} \cap D_f is [f(x‾),f(x‾)][f(\underline{x}), f(\overline{x})] (swapped for decreasing ff), so only the endpoints are evaluated, the lower one rounded down and the upper one rounded up. A domain boundary inside the interval is replaced by the limit there (ln⁡ξ→−∞\ln \xi \to -\infty as ξ↓0\xi \downarrow 0).

  • Functions with known extrema. cosh has its minimum 1 at 0. pown with even exponent has minimum 0 at 0. sin attains +1+1 at ξ=kπ/2\xi = k\pi/2 with k≡1(mod4)k \equiv 1 \pmod 4 and −1-1 at k≡3k \equiv 3; cos attains +1+1 at k≡0k \equiv 0 and −1-1 at k≡2k \equiv 2. Between two consecutive critical points both functions are monotone, so with K={k∈Z:kπ/2∈x}K = \{k \in \mathbb{Z} : k\pi/2 \in \boldsymbol{x}\}

    sin⁡(x)⊆[m,M],m={−1∃k∈K, k≡3min⁡(sin⁡x‾,sin⁡x‾)otherwise,M={1∃k∈K, k≡1max⁡(sin⁡x‾,sin⁡x‾)otherwise.\sin(\boldsymbol{x}) \subseteq [m, M], \qquad m = \begin{cases} -1 & \exists k \in K,\ k \equiv 3 \\ \min(\sin\underline{x}, \sin\overline{x}) & \text{otherwise,} \end{cases} \qquad M = \begin{cases} 1 & \exists k \in K,\ k \equiv 1 \\ \max(\sin\underline{x}, \sin\overline{x}) & \text{otherwise.} \end{cases}

    The code encloses KK by [⌈q−(x‾)⌉,⌊q+(x‾)⌋][\lceil q^-(\underline{x}) \rceil, \lfloor q^+(\overline{x}) \rfloor], where q−≤2ξ/π≤q+q^- \le 2\xi/\pi \le q^+ are computed from a certified enclosure [π−,π+][\pi^-, \pi^+] of π\pi. This set can only be too large: a spurious critical point widens the result to ±1\pm 1, a missed one would break inclusion. When it has four or more elements every residue occurs. sinpi, cospi and tanpi use the exact critical points k/2k/2, computed from the dyadic endpoints without any approximation of π\pi.

  • Poles. tan⁡\tan is continuous and increasing between consecutive poles (2k+1)π/2(2k+1)\pi/2. If KK contains an odd index the interval may contain a pole and the result is Entire; otherwise it is [tan⁡x‾,tan⁡x‾][\tan\underline{x}, \tan\overline{x}]. For tanpi, where the pole positions are exact, a pole at an endpoint gives a half-unbounded result instead. Negative powers and division handle the pole at 0 by the rules of the previous sections.

pow(x, y) on the domain ξ>0\xi > 0 (and ξ=0\xi = 0, η>0\eta > 0) writes ξη=exp⁡(ηu)\xi^\eta = \exp(\eta u) with u=ln⁡ξu = \ln \xi. The map (u,η)↦ηu(u, \eta) \mapsto \eta u is bilinear, so by the corner argument used for products its extrema over the box [ln⁡x‾,ln⁡x‾]×y[\ln \underline{x}, \ln \overline{x}] \times \boldsymbol{y} are at the corners, and exp⁡\exp is increasing: the extrema of ξη\xi^\eta are among the four corner values. When x‾=0\underline{x} = 0, u→−∞u \to -\infty and the corner values become the limits 00 (for η>0\eta > 0) and +∞+\infty (for η<0\eta < 0). The code also adds the value 1 when the box crosses ξ=1\xi = 1 or η=0\eta = 0; such a box contains a point where ηu=0\eta u = 0, so e0=1e^0 = 1 already lies between the extreme corner values and the extra candidate is redundant but harmless.

atan2 is handled the same way: the result is [−π,π][-\pi, \pi] when the box crosses the branch cut on the negative ξ\xi axis, and otherwise the extrema over the box are at corners, as the next section shows.

One evaluation per bound

A bound of an interval result needs one directed evaluation, not the two (down and up) of which half used to be discarded, and often not even a second endpoint. Directed rounding is monotone: if a≥ba \ge b then RD(a)≥RD(b)\mathrm{RD}(a) \ge \mathrm{RD}(b) and RU(a)≥RU(b)\mathrm{RU}(a) \ge \mathrm{RU}(b). So the lower bound min⁡iRD(f(ci))\min_i \mathrm{RD}(f(c_i)) over candidate points cic_i equals RD(f(c∗))\mathrm{RD}(f(c^*)) where c∗c^* minimises ff itself, and likewise for the upper bound. The results are therefore bit-identical to the evaluation of every candidate in both directions; only the argument that c∗c^* is the minimiser has to be written down, once per case.

OperationEvaluations beforeAfter
interval division4 div_ctx2 (one directed quotient per bound)
sqrt4 sqrt_ctx2
product, one factor straddling zero, the other sign-definite4 products2
product with an unbounded factor8 products4 (the coefficient product of a finite pair is shared by both directions)
pown, even exponent, negative or zero-containing interval4 or 3 pown_ctx2
try_atan28 to 16 try_atan2_ctx2
try_sinpi, try_cospi42 (also 2 with one extremum inside, 0 with an extremum of each kind)
try_sin, try_cos8 exact-rational series per refinement round, plus π\pi per round2 correctly rounded binary evaluations, and one π\pi enclosure
try_tan8 series per round, plus π\pi per round2 evaluations, and one π\pi enclosure

Division and square root. The lower bound of x/y\boldsymbol{x} / \boldsymbol{y} is one quotient of two endpoints (chosen by the signs, see the corner argument above) rounded down, the upper bound one quotient rounded up. The square root needs x‾\sqrt{\underline{x}} rounded down and x‾\sqrt{\overline{x}} rounded up.

Products. If a\boldsymbol{a} straddles zero and b≥0\boldsymbol{b} \ge 0 (b≠[0,0]\boldsymbol{b} \ne [0, 0]), then a↦aba \mapsto ab is nondecreasing for each b≥0b \ge 0 and b↦abb \mapsto ab is nondecreasing for a≥0a \ge 0 and nonincreasing for a≤0a \le 0, so the minimum is a‾ b‾\underline{a}\,\overline{b} and the maximum a‾ b‾\overline{a}\,\overline{b} (the other three sign cases are mirror images). Two factors that both straddle zero keep the four products.

atan2. Away from the origin and the branch cut, ∂θ/∂y=x/r2\partial \theta / \partial y = x / r^2 and ∂θ/∂x=−y/r2\partial \theta / \partial x = -y / r^2. For y≥0y \ge 0 the angle is nonincreasing in xx, so the minimum has x=x‾x = \overline{x} and the maximum x=x‾x = \underline{x}; at that xx the angle is nondecreasing in yy if x>0x > 0, nonincreasing if x<0x < 0, and constant (π/2\pi/2) at x=0x = 0. For y≤0y \le 0 (with y‾<0\underline{y} < 0) the angle is nondecreasing in xx and the same rule applies to yy. If y\boldsymbol{y} straddles zero the branch-cut case has been excluded, so x‾≥0\underline{x} \ge 0 and both bounds sit at x=x‾x = \underline{x} (the minimum at y‾\underline{y}, the maximum at y‾\overline{y}). A zero endpoint of y\boldsymbol{y} belongs to the side of the other endpoint: atan2(+0,x<0)=π\mathrm{atan2}(+0, x < 0) = \pi is the limit from y>0y > 0 and atan2(0,x>0)=0\mathrm{atan2}(0, x > 0) = 0 the limit from y<0y < 0. Every edge of the box is therefore monotone and the extrema over a box are at corners, also when the origin is a corner or lies on an edge, since the angles seen near the origin are those of the neighbouring corners. The axis points that earlier versions added are between these two extremes. A box whose extreme corner is the origin (a degenerate box) uses the general corner hull.

sinpi, cospi, sin, cos. With the index range KK of the previous section, an extremum of each kind inside gives ±1\pm 1 without evaluation. Without any extremum inside, x\boldsymbol{x} lies in one arc between two consecutive extrema and the function is monotone there: sine increases through an index ≡0(mod4)\equiv 0 \pmod 4 and decreases through ≡2\equiv 2 (when KK contains an index), and otherwise the arc is k=⌈q−⌉−1k = \lceil q^- \rceil - 1, increasing for k≡0,3k \equiv 0, 3 and decreasing for k≡1,2k \equiv 1, 2. Cosine is sine shifted by one index. The lower bound is then the endpoint value rounded down at the lower end of the arc direction and the upper bound the other endpoint rounded up; with one extremum inside, the opposite bound is the better of the two endpoints.

sin, cos, tan (try_ forms). Each endpoint value is the correctly rounded value of the binary kernel in the directed mode, which is the value the exact-rational series produced (it rounded an enclosure that rounded uniquely). The enclosure of π\pi is needed only for the index range KK (the critical points and the poles); it is taken once from certified_pi_interval with w+48w + 48 bits, where w=p+m+96w = p + m + 96 is the working precision of the first refinement round (pp the precision of the interval, mm the binade of the larger endpoint). Its width, about 2−(w+47)2^{-(w + 47)}, is smaller than that of the rational enclosure of the old refinement loop (about 2−(w+19)2^{-(w + 19)}), so KK is contained in the first-round range of the old code. The old code decided a pole at its first round too, so try_tan returns the whole line only where the old code did. What is identical by construction: every endpoint value, and the result whenever the old code needed no refinement round. What is not: when the old code refined (an endpoint within about 2−w2^{-w} of a multiple of π/2\pi/2) it used a narrower KK of a later round, and the new KK can then hold a spurious index, only for ∣ξ−kπ/2∣<2−(p+140)|\xi - k\pi/2| < 2^{-(p + 140)} (an absolute bound that does not depend on the number of bits of the endpoint). A spurious extremum does not change a sine or cosine bound, since that close to an extremum the directed value already rounds to ±1\pm 1; a spurious pole makes the tangent the whole line, which keeps the inclusion property. The tests compare old and new bit for bit on the binary64 number closest to a multiple of π/2\pi/2 (6381956970095103⋅27976381956970095103 \cdot 2^{797}) and on the correctly rounded π/2\pi/2 and 3π/23\pi/2 with their neighbours at 24, 53 and 113 bits, and no input differs. The non-try sin, cos and tan are these try_ forms with a fallback: they return [−1,1][-1, 1] (Entire for tan) for an argument the kernel cannot certify, which the try_ forms report as an error.

Certified evaluation of transcendental endpoints

ex‾e^{\underline{x}} is not a dyadic number, so an endpoint needs a directed rounding of the exact value: RD⁡p(f(a))\operatorname{RD}_p(f(a)) is a valid lower endpoint and RU⁡p(f(b))\operatorname{RU}_p(f(b)) a valid upper endpoint by property (i). This package does not implement the series itself. Each endpoint is evaluated by the certified try_*_ctx function of binary in an unbounded context of the interval’s precision, rounded toward −∞-\infty for a lower bound and toward +∞+\infty for an upper bound; those functions refine a working precision until the two directed roundings agree (a Ziv-style acceptance test33 A. Ziv, “Fast evaluation of elementary mathematical functions with correctly rounded last bit”, ACM TOMS 17(3), 1991; J.-M. Muller et al., Handbook of Floating-Point Arithmetic, 2nd ed., Birkhäuser, 2018, §10. ) and report a certification failure when the work budget runs out.

What this package adds is the interval analysis around the point kernels: the domain restriction (an argument outside the domain is dropped, and the result is Empty when nothing is left, which is decided by comparisons before any kernel runs), the monotonicity and critical-point analysis of the previous section (which endpoint gives which bound, where an extremum of sin or a pole of tan lies), the handling of infinite endpoints, and the fallback range. An enclosure of π\pi is needed only to locate critical points and poles of the trigonometric functions, and for the fallbacks of the inverse trigonometric functions; it comes from the cached binary-splitting constant of binary (atan(+inf) is π/2\pi/2 correctly rounded in each direction, and the scaling by 2 is exact).

The IEEE 1788 decoration model

A bare interval result says where the values lie, not whether the function was defined. IEEE 1788 attaches a decoration to each result of evaluating ff on a box x\boldsymbol{x}:

DecorationProperty pd(f,x)p_d(f, \boldsymbol{x})
comx\boldsymbol{x} is non-empty and bounded, x⊆Df\boldsymbol{x} \subseteq D_f, ff is continuous at each point of x\boldsymbol{x}, and the result is bounded
dacx\boldsymbol{x} is non-empty, x⊆Df\boldsymbol{x} \subseteq D_f and f∣xf \vert_{\boldsymbol{x}} is continuous
defx\boldsymbol{x} is non-empty and x⊆Df\boldsymbol{x} \subseteq D_f
trvalways true
illthe value is NaI, not an interval

The decorations are totally ordered by strength, com>dac>def>trv>ill\text{com} > \text{dac} > \text{def} > \text{trv} > \text{ill}, because each property implies the next (continuity of ff at each point of x\boldsymbol{x} implies continuity of the restriction, but not conversely: atan2 restricted to a box lying on its branch cut from above is continuous, while atan2 is not). A decorated operation gg applied to decorated inputs (yj,dj)(\boldsymbol{y}_j, d_j) returns

d=min⁡(d1,…,dk,dg),dg=strongest d with pd(g,y1×⋯×yk).d = \min(d_1, \dots, d_k, d_g), \qquad d_g = \text{strongest } d \text{ with } p_d(g, \boldsymbol{y}_1 \times \cdots \times \boldsymbol{y}_k).

Why the minimum is sound. Suppose yj\boldsymbol{y}_j was produced by fjf_j on x\boldsymbol{x} with property pdjp_{d_j}, and gg has pdgp_{d_g} on the box of the yj\boldsymbol{y}_j. If d≥defd \ge \text{def}, each fjf_j is defined on x\boldsymbol{x} with values in yj\boldsymbol{y}_j (inclusion property), and gg is defined on those values, so g∘(f1,…,fk)g \circ (f_1, \dots, f_k) is defined on x\boldsymbol{x}. If d≥dacd \ge \text{dac}, the same holds for continuity, because a composition of continuous functions is continuous. Boundedness for com is a property of the final result and is checked on it. By induction the decoration of a whole expression is a true statement about the expression on the input box. This is what interval existence proofs need: for example, if x\boldsymbol{x} is bounded and F(x)⊆xF(\boldsymbol{x}) \subseteq \boldsymbol{x} with decoration at least dac, the function is continuous on the compact non-empty interval x\boldsymbol{x} and maps it into itself, so Brouwer’s theorem gives a fixed point in x\boldsymbol{x}. Boundedness matters: ξ↦ξ+1\xi \mapsto \xi + 1 maps Entire into itself, continuously, without a fixed point. Without the decoration, [−1,4]=[0,2]\sqrt{[-1, 4]} = [0, 2] would wrongly suggest that ⋅\sqrt{\cdot} is defined on [−1,4][-1, 4].

The package computes dgd_g from the operands by the domain tests listed in the API reference: division by an interval containing 0, a logarithm reaching ξ≤0\xi \le 0, sqrt below 0, a pole of tanpi or of a negative-degree rootn in the argument and so on give trv; atan2 across its branch cut gives def (defined but discontinuous) and touching the cut from above gives dac. Set operations (intersection, convex_hull, cancel_*) are not point functions and always give trv. The result is made canonical: an empty result is always trv (the properties above require a non-empty box), and com on an unbounded result becomes dac (this is how overflow is reported). NaI, the result of an invalid decorated construction, absorbs every operation, and is distinct from ∅\emptyset, which is a valid set.

Integer functions

sign, ceil, floor, trunc, roundTiesToEven and roundTiesToAway (IEEE 1788-2015 Table 9.1) are nondecreasing with integer values, so the bare result is [f(x‾),f(x‾)][f(\underline{x}), f(\overline{x})] and no rounding happens. Their decoration needs one observation: a function with values in Z\mathbb{Z} (or {−1,0,1}\{-1, 0, 1\}) restricted to an interval is continuous exactly when it is constant on it, by the intermediate value theorem. So the strongest decoration dgd_g of 11.2 is def when f(x‾)≠f(x‾)f(\underline{x}) \ne f(\overline{x}). When ff is constant, dac holds, and com holds when in addition x\boldsymbol{x} is bounded and contains no point where ff is discontinuous on the real line. With k=f(x‾)=f(x‾)k = f(\underline{x}) = f(\overline{x}) that last test is an endpoint comparison, because a constant box lies in a single step of the function:

FunctionPoints of discontinuityx\boldsymbol{x} contains one iff
ceilintegers, ceil⁡(n)=n\operatorname{ceil}(n) = nx‾=k\overline{x} = k
floorintegers, floor⁡(n)=n\operatorname{floor}(n) = nx‾=k\underline{x} = k
truncnonzero integersk>0k > 0: x‾=k\underline{x} = k; k<0k < 0: x‾=k\overline{x} = k; k=0k = 0: never
sign00k=0k = 0
roundTiesToEven, roundTiesToAwayn+1/2n + 1/2x‾=k−1/2\underline{x} = k - 1/2 or x‾=k+1/2\overline{x} = k + 1/2

This is why the ITF1788 corpus has ceil([1.1, 2.0]_com) = [2, 2]_dac (the restriction is constant, but 22 is a point of discontinuity) next to floor([-1.2, -1.1]_com) = [-2, -2]_com. An unbounded box is never com, and is def for ceil, floor, trunc and the roundings (the function is not constant on it), dac for sign on a half line that excludes 0. The corpus’s 55 decorated integer-function cases and the tests of the package (each function with the jump as the lower endpoint, the upper endpoint, a point box and in the interior) agree with this rule.

Reverse multiplication and two-output division

c/setb\boldsymbol{c} /_{\text{set}} \boldsymbol{b} (IEEE 1788-2015 10.5.5) is computed by cases on whether 0∈c0 \in \boldsymbol{c} and 0∈b0 \in \boldsymbol{b}. Both intervals are nonempty (otherwise the set is empty).

0∈c0 \in \boldsymbol{c}0∈b0 \in \boldsymbol{b}Set
yesyesR\mathbb{R}: x⋅0=0∈cx \cdot 0 = 0 \in \boldsymbol{c} for every xx
anynothe interval quotient c/b\boldsymbol{c} / \boldsymbol{b} (closed endpoints; the endpoint 0 of an unattained limit is open when 0∉c0 \notin \boldsymbol{c})
nob={0}\boldsymbol{b} = \{0\}empty
no0∈b0 \in \boldsymbol{b}, b≠{0}\boldsymbol{b} \ne \{0\}only y≠0y \ne 0 contributes: one or two components below

In the last row let nn be the end of c\boldsymbol{c} nearest to zero. The divisor values y∈[b‾,0)y \in [\underline{b}, 0) give x≤n/b‾x \le n / \underline{b} when c>0\boldsymbol{c} > 0 and x≥n/b‾x \ge n / \underline{b} when c<0\boldsymbol{c} < 0; the values y∈(0,b‾]y \in (0, \overline{b}] give x≥n/b‾x \ge n / \overline{b} when c>0\boldsymbol{c} > 0 and x≤n/b‾x \le n / \overline{b} when c<0\boldsymbol{c} < 0. A divisor side exists only when b‾<0\underline{b} < 0, respectively b‾>0\overline{b} > 0. The quotient n/±∞n / \pm\infty is 0 and is the unattained end of the component, so that component is open there. Each finite quotient is rounded outward, which keeps the closure a valid enclosure and tight in the type (the exact endpoint is attained, because y=b‾y = \underline{b} or b‾\overline{b} is in b\boldsymbol{b}).

mul_rev_to_pair closes the components. mul_rev(b, c, x) intersects each component, with its open ends, with the closed interval x\boldsymbol{x} and takes the hull of what is nonempty (a lower end LL and upper end UU meet when L<UL < U, or L=UL = U and both ends are closed). This is why the hull of the set and the intersection of x\boldsymbol{x} with its hull differ: for b=[−1,1]\boldsymbol{b} = [-1, 1], c=[1,1]\boldsymbol{c} = [1, 1] the set avoids (−1,1)(-1, 1), so the prior [0,0.5][0, 0.5] gives Empty. All 533 corpus cases of mulRev, mulRevTen and mulRevToPair agree with the construction.

Reverse functions of even and monotone algebraic functions

sqr_rev, abs_rev, cosh_rev and pown_rev (IEEE 1788-2015 10.5.4) share one scheme. The preimage of c\boldsymbol{c} under φ\varphi is a union of at most two intervals, found from the inverse of φ\varphi at the ends of c\boldsymbol{c} after cutting c\boldsymbol{c} to the range of φ\varphi:

  • for an even φ\varphi (x2x^2, ∣x∣\lvert x\rvert, cosh⁡\cosh, an even xpx^p) with inverse rr on the positive side, and c′=c∩range\boldsymbol{c}' = \boldsymbol{c} \cap \text{range}: the two pieces [−r(c′‾),−r(c′‾)][-r(\overline{c'}), -r(\underline{c'})] and [r(c′‾),r(c′‾)][r(\underline{c'}), r(\overline{c'})];
  • for an odd xpx^p with p>0p > 0, increasing: [r(c‾),r(c‾)][r(\underline{c}), r(\overline{c})];
  • for a negative power, xpx^p is decreasing on each side of the pole 0, so the roles of the two ends swap, x=0x = 0 is excluded, and the end of a piece at 0 is open: the unbounded end of c\boldsymbol{c} gives r=0r = 0, which is only a limit.

Each finite end is one directed rounding of the exact root of one exact endpoint (sqrt, rootn, acosh of a point interval), so the closed pieces are the tightest in the type. The hull with the prior x\boldsymbol{x} meets each piece, with its open ends, with x\boldsymbol{x} and takes the hull of the nonempty parts, the same routine as mul_rev. The trivial decoration (trv) is attached by the decorated forms. All 360 corpus cases of these functions in libieeep1788_tests_rev.itl and abs_rev.itl agree.

Reverse sine, cosine and tangent

sin_rev, cos_rev and tan_rev (IEEE 1788-2015 10.5.4) return the hull of the part of a periodic union of intervals that lies in the prior x\boldsymbol{x}. A piece end has the form t=m2π+σ α(s)t = \tfrac{m}{2}\pi + \sigma\,\alpha(s) with an integer mm, σ=±1\sigma = \pm 1 and α\alpha one of arcsin⁡\arcsin, arccos⁡\arccos, arctan⁡\arctan of an endpoint ss of c\boldsymbol{c} (a pole of tan⁡\tan has no α\alpha). The result takes the piece ends that matter, so the comparison of a prior end with a piece end has to be exact; a wrong answer there moves a result end by a whole period (for example sin_rev([0, 0], x = [RU(pi), 6])).

Which piece ends can be floats. If t=dt = d with dd a nonzero float, then dd is algebraic, and so is ss. For sin⁡d=±s\sin d = \pm s the Lindemann-Weierstrass theorem says eide^{id} is transcendental, so sin⁡d\sin d cannot be algebraic; the same holds for cos⁡d\cos d and tan⁡d\tan d. Every end is therefore irrational (a pole ±π/2+πk\pm\pi/2 + \pi k and the ends from s=±1s = \pm 1 are nonzero rational multiples of π\pi) except t=0t = 0, which needs m=0m = 0 and α=0\alpha = 0: arcsin⁡0\arcsin 0 and arctan⁡0\arctan 0, and arccos⁡1\arccos 1. Those ends are compared with the prior by an exact test, and every other end has a strict answer for every float dd. A pole therefore never equals a float, and the trigonometric reverse functions need no open flag.

Comparison and rounding. Each end is evaluated as an enclosure at a working precision ww, starting at p+b+96p + b + 96 bits (bb the binary exponent of the larger prior end), from the certified enclosure of π\pi and the certified asin, acos, atan of the exact point ss. A comparison with a prior end dd is decided as soon as the enclosure excludes dd, else ww is doubled. The value written for a result end is the outward rounding of an enclosure that contains no float of the result precision, which is exactly RD⁡(t)\operatorname{RD}(t) or RU⁡(t)\operatorname{RU}(t); here too ww is doubled until that holds. The first step is enough for almost every input, and not for all: a float can lie within a small fraction of an ulp of a multiple of π/2\pi/2, which is why the loop is needed. The loop is capped at 12 doublings (the same budget as the certified forward functions). At the cap an undecided comparison counts as met in both conditions (a piece that might meet the prior is never dropped) and the end written for such a piece is the prior’s own end, a valid bound of both possibilities; a rounding is that of the last enclosure. The result is then an enclosure of the hull, possibly wider. A white-box test sets the cap to 0 and to 1 and checks, on cases of the reference test, that the result contains the hull and stays inside the prior. The pinned ITF1788 corpus never leaves the first step: its 136 cases need w=p+b+96w = p + b + 96 at most.

Pieces and indices. A piece [lo(k),hi(k)][\mathrm{lo}(k), \mathrm{hi}(k)] belongs to a family with m=offset+period⋅km = \text{offset} + \text{period} \cdot k (in units of π/2\pi/2, the period is 4 for sin⁡\sin and cos⁡\cos and 2 for tan⁡\tan). The first piece that reaches the lower prior end xlx_l has its upper end in a window of a dozen consecutive values of mm around 2xl/π2 x_l/\pi, which is computed exactly in rational arithmetic from the lower and upper bound of the enclosure of π\pi, widened by a stated margin: an end lies in [(m−2)π/2,(m+2)π/2][(m-2)\pi/2, (m+2)\pi/2] since ∣α∣≤π\lvert\alpha\rvert \le \pi, so a piece below the window cannot reach xlx_l and the predecessor of a piece above it already does. The same holds at xhx_h with the lower ends. Every piece of the window is tested for meeting the prior (hi >= x_l and lo <= x_h, both exact), the lower end of the result is the minimum over the met pieces of xlx_l or RD⁡(lo)\operatorname{RD}(\mathrm{lo}), and the upper end the maximum of xhx_h or RU⁡(hi)\operatorname{RU}(\mathrm{hi}); no two transcendental ends are compared with each other, since RD⁡\operatorname{RD} is monotone. The work is two searches of a few pieces each, whatever the prior spans, and its cost follows the number of bits of the period index through the working precision. The only limit is the one of the forward functions: an end of the prior at or beyond 2max⁡(65536,4p)+12^{\max(65536, 4p)+1} is kept (valid, not tight). It is decided before any work (the working precision comes only from the ends inside it, and a prior whose finite ends are all beyond it is returned at once), so such an end never costs π\pi at its precision.

Tightness against the corpus. The tests compare the result with an enumeration of every piece (trig_reverse_reference_wbtest.mbt), with no index arithmetic in common with the implementation, over 4,200 random cases at precisions 24 and 53 that include priors with an end next to a piece end. Six distinct corpus cases (twelve with the decorated copies) have an expected value that is one or two ulps wider than the hull; they are listed on the conformance page.

Reverse power with an interval exponent

pow_rev1(b, c, x) (IEEE 1788-2015 10.5.4) reduces to the set division of mul_rev in logarithm coordinates (see the API page): the pieces of exp⁡(L/setb)\exp(L /_{\text{set}} \boldsymbol{b}) come from the same case split as c/setbc /_{\text{set}} b (whether 0∈L0 \in L, that is 1∈c1 \in \boldsymbol{c}, and whether 0∈b0 \in \boldsymbol{b}), with the sign of an end of LL decided by comparing the end of c\boldsymbol{c} with 1. The end x=0x = 0 of a piece is open (it comes from ln⁡x→−∞\ln x \to -\infty); the zero part {0}\{0\} is a piece of its own; x=1x = 1 is open when it is the limit of a quotient by an unbounded b\boldsymbol{b} and attained when cend=1c_\text{end} = 1; every other finite end is attained.

Exactness of an end x=c1/βx = c^{1/\beta}, c>0c > 0, β≠0\beta \ne 0 finite. Write β=s u 2j\beta = s\,u\,2^j and c=C 2γc = C\,2^\gamma with uu and CC odd, p=2max⁡(−j,0)p = 2^{\max(-j, 0)} and q=u 2max⁡(j,0)q = u\,2^{\max(j, 0)}. Then x=csp/qx = c^{s p / q} with gcd⁡(p,q)=1\gcd(p, q) = 1, and xx is rational iff cc is a perfect qq-th power in Q\mathbb{Q}, that is C=mqC = m^q and q∣γq \mid \gamma (for C>1C > 1 this needs q≤bitlen⁡(C)q \le \operatorname{bitlen}(C), a comparison, and then one integer root; for C=1C = 1 it needs q∣γq \mid \gamma, a divisibility test; no power of cc is formed). The value is then (m 2γ/q)sp(m\,2^{\gamma/q})^{s p}. There are three classes of end and each has its own line in the procedure:

ClassWhenComparison with a prior endRounding
dyadics=+1s = +1 or m=1m = 1exact (it is mp2…m^p 2^{\ldots})exact with_precision
rational, not dyadics=−1s = -1 and m>1m > 1, for example 91/(−2)=1/39^{1/(-2)} = 1/3exact cross-multiplicationinteger division with directed rounding
irrationalotherwisethe loop belowthe loop below

A rational that is not dyadic is never a float, so its comparisons are strict without any refinement. An irrational value is not rational, so it differs from every float, and the loop terminates: the working precision starts at p+64+etp + 64 + e_t, with ete_t the exponent of t=ln⁡(c)/βt = \ln(c)/\beta (the result needs the absolute error of tt below 2−p2^{-p}), and doubles up to the cap of 12 as in sin_rev; at the cap an undecided comparison counts as met in both conditions and the end written for that piece is the prior’s own end. The sign of tt is exact, so exp⁡(t)>1\exp(t) > 1 iff t>0t > 0: the enclosure is strict at the float 1, and an exponent such as β=2109\beta = 2^{10^9}, where x=1+2−109ln⁡cx = 1 + 2^{-10^9}\ln c, is decided at once (RD⁡=1\operatorname{RD} = 1, RU⁡=1+ulp\operatorname{RU} = 1 + \text{ulp}). The stored range is decided with a certified upper bound of ln⁡2\ln 2, by a test that is sufficient and never wrong. Above: every finite float is below 2emax⁡+12^{e_{\max}+1} (emax⁡e_{\max} is the exponent of the leading bit), so if the enclosure of tt lies wholly at or above (emax⁡+1)ln⁡2(e_{\max}+1)\ln 2 the enclosure of the value is [largest finite float at ww bits, +∞+\infty], built explicitly, and a lower end then rounds down to the largest finite float of the result precision; the top binade [2emax⁡,2emax⁡+1)[2^{e_{\max}}, 2^{e_{\max}+1}) is finite and goes through exp⁡\exp. Below: the stored values at ww bits go down to the leading bit emin⁡−w+1e_{\min}-w+1, so if the enclosure of tt lies wholly at or below (emin⁡−w)ln⁡2(e_{\min}-w)\ln 2 the enclosure is [0,2emin⁡−w+1][0, 2^{e_{\min}-w+1}], the smallest positive stored value at ww bits, and an upper end rounds up to the smallest positive stored value of the result precision. Between the limits exp⁡\exp is used, with gradual underflow (the enclosure is at most one quantum 2emin⁡−w+12^{e_{\min}-w+1} wide). Interval::exp of an interval with an end past the range returns the range of the function [0,+∞][0, +\infty] at some precisions (issue #569); the point exp of the lower end is a tight bound, so the two ends give the enclosure. If the enclosure of tt straddles the upper limit, exp⁡\exp returns an infinite upper end; the loops stop with it only if its lower end is checked to be at least 2emax⁡+1(1−2−(w−64))2^{e_{\max}+1}(1 - 2^{-(w-64)}) (the largest finite float at w−64w - 64 bits), which says exactly that the upper end is +∞+\infty and the lower end rounds down to the largest finite float of the result at every precision p≤w−64p \le w - 64, hence at every further precision; otherwise the loops go on to their cap. If it straddles the lower limit the loops go on and terminate, since the quantum shrinks with ww while the value is fixed. An exact dyadic end in the subnormal range is kept exact, at a datum precision of at least emin⁡−low+1e_{\min} - \text{low} + 1 bits within pow_value_budget, where low\text{low} is the exponent of the coefficient’s lowest bit (the stored values at PP bits are the multiples of 2emin⁡−P+12^{e_{\min}-P+1}; the leading-bit formula is too small for a coefficient of more than one bit), and the datum built is checked to carry the value asked (an exact class never carries a rounded value) (for example 2−2302^{-2^{30}} is the stored value 2emin⁡−22^{e_{\min}-2}, and 3⋅2emin⁡−53 \cdot 2^{e_{\min}-5} needs 6 bits; the result for a point set is [v,v][v, v] for every prior that contains it, in about 0.2 s); an exact end above 2emax⁡+12^{e_{\max}+1} (for example 2(230)2^{(2^{30})}), a rational end in the subnormal range, and a dyadic end below the budget are decided by the loop, which clamps at the limits (a rational is never a float, so its separation terminates at the first precision).

Budgets, in bits and not in terms of 53: the exactness test runs only for CC and qq within pow_int_budget = 65,536 bits (an exact 65,535-bit square takes about 80 ms, against about 20 ms for the forward sqrt of the same operand) and an exact value is materialised only within pow_value_budget = 262,144 bits (a value of millions of bits would be built only to be rounded to pp bits). Above a budget the end goes to the loop with its documented fallback. The corpus needs none of this: its 429 cases need the first working precision of the loop at most (w=p+64=117w = p + 64 = 117 at p=53p = 53, measured), and the oracle reproduces all of them.

Tests. The definition tests cover the classes (including a 2000-bit perfect square and a β\beta with 500 fractional bits), the open ends at 0 and 1 against a prior that touches them from both sides, and exponents and prior ends of magnitude 2±1092^{\pm 10^9}. The reference test (pow_reverse_reference_wbtest.mbt) does not use the pieces: it evaluates the condition for a float xx with the forward Interval::pow at 200 bits, since β↦xβ\beta \mapsto x^\beta is monotone and its range over b\boldsymbol{b} is the interval pow(x, b), and checks, at precisions 24, 53 and 113, that every float certainly in the set is in the result, that a point a quarter of a float outside either end that comes from a piece is not in the set, and that the float one step inside is not certainly outside while the next one is certainly inside. A sample runs in every test run; the long run is an interval gate stage.

Reverse power with an interval base

pow_rev2(a, c, x) (IEEE 1788-2015 10.5.4) is the same set division with the roles exchanged (see the API page): the unknown is the exponent, and the set is L/setML /_{\text{set}} M with L=ln⁡(c∩(0,+∞))L = \ln(\boldsymbol{c} \cap (0, +\infty)) and M=ln⁡(a∩(0,+∞))M = \ln(\boldsymbol{a} \cap (0, +\infty)), plus (0,+∞)(0, +\infty) when 0∈a0 \in \boldsymbol{a} and 0∈c0 \in \boldsymbol{c}. The pieces come from the same case split as pow_rev1 (whether 1∈c1 \in \boldsymbol{c} and whether 1∈a1 \in \boldsymbol{a}, the signs of the logarithms being comparisons with 1), and a piece end is the quotient ln⁡(cend)/ln⁡(aend)\ln(c_\text{end}) / \ln(a_\text{end}), with the infinities and the open 0 as in pow_rev1 (here no value is mapped through exp⁡\exp, so there is no end at 1).

Exactness of an end r=ln⁡(C)/ln⁡(A)r = \ln(C)/\ln(A) for floats A≠1A \ne 1 and C>0C > 0. Write A=α 2iA = \alpha\,2^i and C=γ 2jC = \gamma\,2^j with α\alpha and γ\gamma odd and positive (the exponents ii and jj carry the sign of the logarithms). rr is rational iff AA and CC are multiplicatively dependent, and then:

CaseEnd
C=1C = 1r=0r = 0
α=γ=1\alpha = \gamma = 1r=j/ir = j / i
exactly one of α\alpha, γ\gamma is 1irrational (C≠1C \ne 1)
both exceed 1the multiplicative Euclidean algorithm gives BB with α=Bi′\alpha = B^{i'}, γ=Bj′\gamma = B^{j'} and gcd⁡(i′,j′)=1\gcd(i', j') = 1; r=j′/i′r = j'/i' iff i j′=j i′i\,j' = j\,i', else irrational

The algorithm divides the larger odd integer by a power of the smaller, yky^k with k=⌊(bitlen⁡(x)−1)/bitlen⁡(y)⌋k = \lfloor (\operatorname{bitlen}(x) - 1) / \operatorname{bitlen}(y) \rfloor (so that yk≤xy^k \le x; it divides xx whenever both are powers of one base), and an inexact division proves independence; a high power of a small base (33 against 3400003^{40000}) takes a few steps, not one per factor. The exponents of BB are recovered by running the steps backwards, and the exact check α=Bi′\alpha = B^{i'}, γ=Bj′\gamma = B^{j'} is made before the value is used. The value j′/i′j'/i' is a float iff the reduced denominator is a power of two (class dyadic), and otherwise a rational that is not a float (compared by exact cross-multiplication, rounded by integer division with directed rounding, the sign taken into account). An odd part beyond pow_int_budget = 65,536 bits gives the class Unknown, decided by the loop like an irrational end.

Working precision. The first working precision of the loop is p+64+erp + 64 + e_r, with ere_r the exponent of the quotient: ln⁡\ln has a relative error of about 2−w2^{-w} also for an argument next to 1 (the enclosure of ln⁡(1+2−52)\ln(1 + 2^{-52}) keeps its full width in bits), so only the integer part of the quotient costs bits. A base one unit from 1 gives a quotient of about 2512^{51}, and the tests check that its enclosure is tight (adjacent floats) at precision 53.

Budgets, in bits: one call of the exactness test at the budget (3413403^{41340} against 33, 65,524 bits) takes about 7 ms, against about 6 ms for the forward pow of the same operand at 200 bits, so the budget of pow_rev1 is kept. The corpus needs none of it: the oracle finds no irrational piece end among its 375 statements (every end is exact, a float or a rational), so the loop and its fallback are covered by the tests below and not by the corpus.

Corpus. Two statements of the corpus expect a value that is valid but wider than the hull (powRev2 [0.25, 0.5] [2, inf] expects Entire and powRev2 [0.25, 1] [2, inf] expects [−∞,0][-\infty, 0], where a0β≥2a_0^\beta \ge 2 with a0<1a_0 < 1 needs β≤−1/2\beta \le -1/2); they are refinements, with the oracle (tools/itl_pow2_reverse_oracle.py, which derives the set from the corner quotients and shares no code with interval) giving 373 equal and 2 refined of 375 statements.

Tests. The definition tests cover the classes (including the Euclid steps, 33 against 3400003^{40000}, and a beyond-budget odd part), exact and rounded rational ends with their sign, the examples of the corpus, the open ends at 0 against a prior that touches them, a base one unit from 1 on both sides, and bases, results and prior ends of magnitude 2±1092^{\pm 10^9}. The reference test (pow_reverse2_reference_wbtest.mbt) evaluates the condition for a float β\beta with the forward Interval::pow at 200 bits (the range of a0βa_0^\beta over a\boldsymbol{a} is the interval pow(a, beta)) and checks, at precisions 24, 53 and 113, that every float certainly in the set is in the result, that a point a quarter of a float outside an end that comes from a piece is not in the set, and that the float one step inside is not certainly outside while the next one is certainly inside. A sample runs in every test run; the long run is an interval gate stage.

Reverse arctangent of two arguments

atan2_rev1(b, c, x) and atan2_rev2(a, c, x) (IEEE 1788-2015 10.5.4, equations (6) and (7), Table 10.1; see the API page). With Θ=c∩(−π,π]\Theta = \boldsymbol{c} \cap (-\pi, \pi] the set is the projection of the sector { r(cos⁡t,sin⁡t):r>0, t∈Θ }\{\, r(\cos t, \sin t) : r > 0,\ t \in \Theta \,\} on the axis of the unknown (the ordinate for Rev1, the abscissa for Rev2), cut by the strip of the given operand: a point of the ray at the angle tt has the ordinate rsin⁡tr \sin t and the abscissa rcos⁡tr \cos t. Away from the axis directions this is the product y=b0tan⁡ty = b_0 \tan t (Rev1) or x0=a0cot⁡tx_0 = a_0 \cot t (Rev2) of an operand point and the trigonometric value, so each piece is an interval product, and the hull is taken over the pieces inside the prior as for pow_rev1. Write b+=b∩(0,∞)b^+ = \boldsymbol{b} \cap (0, \infty) and b−=b∩(−∞,0)b^- = \boldsymbol{b} \cap (-\infty, 0) (likewise a±a^\pm).

OperationPieceAngles ttNeedsSet
Rev1rightΘ∩(−π/2,π/2)\Theta \cap (-\pi/2, \pi/2)b+≠∅b^+ \ne \emptysetb+⋅tan⁡(Θr)b^+ \cdot \tan(\Theta_r)
Rev1upper-leftΘ∩(π/2,π]\Theta \cap (\pi/2, \pi]b−≠∅b^- \ne \emptysetb−⋅tan⁡(Θul)b^- \cdot \tan(\Theta_{ul}), a subset of [0,∞)[0, \infty); tan⁡π=0\tan \pi = 0 is attained
Rev1lower-leftΘ∩(−π,−π/2)\Theta \cap (-\pi, -\pi/2)b−≠∅b^- \ne \emptysetb−⋅tan⁡(Θll)b^- \cdot \tan(\Theta_{ll}), a subset of (−∞,0)(-\infty, 0); the end at −π-\pi is open
Rev1up, downt=π/2t = \pi/2 or t=−π/2t = -\pi/2 in Θ\Theta0∈b0 \in \boldsymbol{b}(0,+∞)(0, +\infty) or (−∞,0)(-\infty, 0)
Rev2upper half-planeΘ∩(0,π)\Theta \cap (0, \pi)a+≠∅a^+ \ne \emptyseta+⋅cot⁡(Θu)a^+ \cdot \cot(\Theta_u) (cot⁡\cot decreases from +∞+\infty to −∞-\infty)
Rev2lower half-planeΘ∩(−π,0)\Theta \cap (-\pi, 0)a−≠∅a^- \ne \emptyseta−⋅cot⁡(Θl)a^- \cdot \cot(\Theta_l); the end at −π-\pi is open
Rev2right, leftt=0t = 0 or t=πt = \pi in Θ\Theta0∈a0 \in \boldsymbol{a}(0,+∞)(0, +\infty) or (−∞,0)(-\infty, 0)

The tangent (cotangent) interval of a piece has one end per end of Θr\Theta_r: the tangent of an end of c\boldsymbol{c} inside the region, or the limit at a boundary of the region: ±∞\pm\infty at ±π/2\pm\pi/2 for the tangent (at 00 and π\pi for the cotangent), 0 at −π-\pi, 0 and π\pi for the tangent. Whether an end of c\boldsymbol{c} is below or above −π-\pi, −π/2-\pi/2, 00, π/2\pi/2 or π\pi is decided exactly, in seven zones (atan2_zone): π\pi is irrational, so a float is never equal to a multiple of it, and only 00 is an exact angle; the enclosure of π\pi is refined until the float is separated. The product of the operand part with the trigonometric interval is monotone in each variable, so each extreme is the product of two ends, chosen by the signs of the ends of the trigonometric interval (no numerical comparison between corners is needed), and an end is attained only if both factors are: the limit 0 of b+b^+ (when 0∈b0 \in \boldsymbol{b}) and the infinities are not attained, tan⁡0=0\tan 0 = 0 and tan⁡π=0\tan \pi = 0 are, and tan⁡(−π)=0\tan(-\pi) = 0 is not.

Branch cut and origin. The angle π\pi is attained and −π-\pi never, so π∈c\pi \in \boldsymbol{c} puts y=0y = 0 into the Rev1 set when b−≠∅b^- \ne \emptyset (an end of the upper-left piece, attained) and x0<0x_0 < 0 into the Rev2 set when 0∈a0 \in \boldsymbol{a}, while a c\boldsymbol{c} with lower end ≤−π\le -\pi gives a lower-left piece whose end at −π-\pi (the limit of tan⁡\tan at −π-\pi) is open. The origin is excluded: for b={0}\boldsymbol{b} = \{0\} there are only the up and down pieces, so y=0y = 0 is not reachable and atan2Rev1⁡({0},{0})\operatorname{atan2Rev1}(\{0\}, \{0\}) is Empty; for a={0}\boldsymbol{a} = \{0\} there are only the angles 00 and π\pi, so x0=0x_0 = 0 is not in the set.

Exactness of an end. The exact ends are 00 and the infinities. Every other end is bendtan⁡(cend)b_\text{end} \tan(c_\text{end}) (Rev1) or aend/tan⁡(cend)a_\text{end} / \tan(c_\text{end}) (Rev2) with a nonzero float operand end and a nonzero float angle, which is irrational: tan⁡\tan of a nonzero algebraic number is transcendental (Lindemann-Weierstrass) and a nonzero rational times a transcendental number is transcendental, so the end differs from every float, comparisons with a prior end are strict, and the loop of pow_rev1 (the working precision doubles up to the cap of 12; an undecided comparison counts as met and the prior’s own end is written) terminates. No integer test and no budget are involved. The first working precision is p+96p + 96: the forward tan of a point has a relative error of about 2−w2^{-w} also next to π/2\pi/2, 00 and π\pi (the argument reduction runs at the working precision), and the loop needs the value to a relative 2−(p+2)2^{-(p+2)}. An angle one float from π\pi gives a cotangent of order 2p2^{p} (the tests check the result is two adjacent floats that contain the 200-bit reference).

Evidence. The ITF1788 corpus has no atan2Rev case, so the evidence is ours. tools/itl_atan2_reverse_oracle.py (pure Python, 260 digits, no interval code) derives the exact hull by polar rectangles (an angle interval times an operand part of one sign, with corner limits) and validates every expected hull by the forward definition of the operation, which shares nothing with the case analysis. For a fixed ordinate yy, atan2⁡(y,b0)\operatorname{atan2}(y, b_0) is monotone and continuous in b0b_0 on each side of 0 (decreasing for y>0y > 0, increasing for y<0y < 0; for y=0y = 0 it is 0 for b0>0b_0 > 0 and π\pi for b0<0b_0 < 0), so its image over b\boldsymbol{b} is an interval decided from the ends of b\boldsymbol{b}, and yy is a member iff the image meets c\boldsymbol{c} (Rev2 likewise, with the ordinate part split at 0). The validation tests an attained end, three points a geometric step inside each piece end (10−910^{-9}, 10−1510^{-15}, 10−3010^{-30} relative) and six points beyond each end of the hull, inside the prior (10−3010^{-30}, 10−1510^{-15}, 10−910^{-9}, 10−310^{-3}, 1 and 100 relative). The generated file testdata/interval/generated/atan2_rev.itl (see its README) has 16,260 cases, run as the atan2-reverse phase, counted apart from the ITF1788 total. The reference test (atan2_reverse_reference_wbtest.mbt) evaluates membership of a float with the forward Interval::atan2 of points at 200 bits (the same monotone image, with the cut treated explicitly) and checks, at precisions 24, 53 and 113, that every float certainly in the set is in the result, that a point a quarter of a float outside an end that comes from a piece is not in the set, and that the float one step inside is not certainly outside while the next one is certainly inside. The maximum working precision on the generated corpus is 149 bits (the first working precision, p+96p + 96 at p=53p = 53; none of the 6,813 evaluations of the loop doubled it).

Interval literals and text input

from_string (IEEE 1788-2015 textToInterval, 9.7, 12.11, 12.12.7, 13.2; see the API page) is a hand-written parser of the portable grammar (Tables 9.5 and 12.2) over the characters of the string. A number literal is read into one of four exact descriptions: an infinity, a decimal d⋅10ed \cdot 10^{e} (the digits without the point and the exponent minus the number of digits after the point), a hexadecimal h⋅2eh \cdot 2^{e} (the exponent minus four times the number of hexadecimal digits after the point), or a rational p/qp/q with qq positive. An exponent field is saturated at 101510^{15} in magnitude (beyond every range). The uncertain form m?rvem?rve is turned into two decimal descriptions by the definition of 9.7.3: ulp⁡(m)=10−k\operatorname{ulp}(m) = 10^{-k} with kk the number of digits after the point of mm, the bounds are (M∓r)10e−k(M \mp r) 10^{e-k} with MM the integer of the digits (one ulp being 10−k10^{-k}; for the empty radius, half an ulp, as (10M∓5)10e−k−1(10M \mp 5) 10^{e-k-1}), the direction uu or dd keeps one side, and the radius ?? is unbounded; the arithmetic is on big integers, so no rounding precedes the final one.

Rounding. Each bound is rounded once, outward and exactly: a decimal and a hexadecimal description go through the correctly rounded directed conversions of Binary (from_string_ctx, from_hex_ctx, which accept any exponent), and a rational is divided by integer division to a quotient of at least p+3p + 3 bits with the sticky remainder (the same rule as the exact rational ends of pow_rev1). A literal whose exact value is beyond the stored range is therefore decided by those conversions from the exponent without forming the value, and 1e400000000 has the lower end the largest finite float and the upper end +∞+\infty. Both converters accept an exponent field of any length (from_hex_ctx saturates it at 2602^{60} like from_string_ctx; before issue 597 it rejected an exponent beyond 101210^{12} and the parser clamped it). The invariants are checked: a failure of a converter or of the digit parser aborts instead of returning a bound.

The order l≤ul \le u. It is decided from the exact values, so rounding never makes a nonempty literal Empty and an empty one nonempty: two decimal bounds are compared by the adjusted exponent and then the digit strings (trailing zeros removed, the shorter padded with zeros), which needs no power of ten whatever the exponent; any other pair is first separated by roundings at 64 bits and otherwise compared by cross-multiplication of the exact numerators and denominators, a power being formed only for an exponent of magnitude at most 65,536. A pair that is neither separated nor within that limit (two bounds of different radices that agree to 64 bits and have an exponent beyond 65,536) is undecided: 12.12.7 case (b) allows it and requires the result to contain both bounds, so the result is the hull of the four roundings (a false positive, which the standard permits; a false negative is not possible).

Rejections. Every failed call is an Err of the parse kind whose message names the rule of the grammar or of 12.11 that was broken (a missing digit, the exponent field of a hexadecimal literal, the denominator of a rational, the order of the bounds, an infinite bound on the wrong side, the characters after the literal, the decoration literal, a decoration not permitted for the value by 11.4, a decoration on a bare literal). The test table asserts the reason of every rejected string. A literal longer than interval_literal_max_length is not parsed and gives Entire (12.11.5).

Evidence. The independent oracle tools/interval_literal_oracle.py (pure Python, no interval code) evaluates a literal from whole-string patterns (a special form, the two bound strings of an inf-sup form cut at the comma, the groups of an uncertain pattern) to an exact Fraction, derives the uncertain form from the definition of the ulp, and rounds each bound with integer arithmetic against the stored range (gradual underflow to the quantum 2emin⁡−p+12^{e_{\min}-p+1}, the largest finite float (2p−1)2emax⁡−p+1(2^p - 1) 2^{e_{\max}-p+1}); it also checks every one of 20,000 rational literals against the rounded ends by cross-multiplication (the rounded lower end is not above the value and the float below it is below, and likewise for the upper end). The generated table (9,919 literals: the vectors of Tables 9.4 and 12.1, the negative examples, and random decimal, hexadecimal, rational, uncertain, decorated and special literals at precisions 24, 53 and 113, bare and decorated) is compared with the parser in interval_literal_wbtest.mbt.

Relations: certainly and possibly

An interval stands for an unknown point, so a comparison of two intervals asks a quantified question. Two quantifiers give the useful relations:

∀ξ∈x,∀η∈y:ξ<η  ⟺  sup⁡x<inf⁡y  ⟺  x‾<y‾definitely_lt∃ξ∈x,∃η∈y:ξ=η  ⟺  x∩y≠∅  ⟺  x‾≤y‾∧y‾≤x‾maybe_eq\begin{aligned} \forall \xi \in \boldsymbol{x}, \forall \eta \in \boldsymbol{y} : \xi < \eta &\iff \sup\boldsymbol{x} < \inf\boldsymbol{y} \iff \overline{x} < \underline{y} && \texttt{definitely\_lt} \\ \exists \xi \in \boldsymbol{x}, \exists \eta \in \boldsymbol{y} : \xi = \eta &\iff \boldsymbol{x} \cap \boldsymbol{y} \ne \emptyset \iff \underline{x} \le \overline{y} \wedge \underline{y} \le \overline{x} && \texttt{maybe\_eq} \end{aligned}

For the first line, "⇐\Leftarrow" is ξ≤x‾<y‾≤η\xi \le \overline{x} < \underline{y} \le \eta. For "⇒\Rightarrow": if x‾\overline{x} and y‾\underline{y} are finite they are elements, so x‾<y‾\overline{x} < \underline{y}; if x‾=+∞\overline{x} = +\infty or y‾=−∞\underline{y} = -\infty, a large ξ\xi or a small η\eta violates ξ<η\xi < \eta, and the endpoint test is false as well. The second line is the nonempty intersection of two intervals. “Possibly less” is the negation of “certainly not less”, so the two families are dual: DefinitelyLt::definitely_lt(x, y) is false exactly when some pair satisfies ξ≥η\xi \ge \eta. With empty operands the universal statements would be vacuously true, which would let a caller prove anything from an empty enclosure; the DefinitelyLt and DefinitelyLe relations (and Overlaps, MaybeEq) therefore return false for empty operands, whereas the IEEE 1788 relation precedes (∀ξ ∀η:ξ≤η\forall\xi\,\forall\eta: \xi \le \eta) is defined to be vacuously true.

The set relations (subset, interior, disjoint, ==) and the IEEE 1788 orders (less: ∀ξ ∃η ξ≤η\forall\xi\,\exists\eta\, \xi \le \eta and ∀η ∃ξ ξ≤η\forall\eta\,\exists\xi\, \xi \le \eta, which reduces to comparing both endpoint pairs) are evaluated by endpoint comparisons in the same way. None of these relations is a total order, and the trait method @lf_arith.Contains::contains(x, y) is set inclusion y⊆x\boldsymbol{y} \subseteq \boldsymbol{x}, matching the enclosure reading of the arithmetic traits.

Design decisions

Endpoints, not midpoint and radius

Problem. An interval can be stored as endpoints [x‾,x‾][\underline{x}, \overline{x}] (inf–sup) or as a midpoint and radius ⟨m,r⟩\langle m, r\rangle, as in Arb.44 J. van der Hoeven, “Ball arithmetic”, 2009; F. Johansson, “Arb: efficient arbitrary-precision midpoint-radius interval arithmetic”, IEEE Trans. Computers 66(8), 2017.

Midpoint–radius arithmetic. With x=mx+δxx = m_x + \delta_x, ∣δx∣≤rx|\delta_x| \le r_x and likewise for yy:

x+y=(mx+my)+(δx+δy),∣δx+δy∣≤rx+ry,xy−mxmy=mxδy+myδx+δxδy,∣xy−mxmy∣≤∣mx∣ry+∣my∣rx+rxry.\begin{aligned} x + y &= (m_x + m_y) + (\delta_x + \delta_y), & |\delta_x + \delta_y| &\le r_x + r_y, \\ xy - m_x m_y &= m_x\delta_y + m_y\delta_x + \delta_x\delta_y, & |xy - m_x m_y| &\le |m_x| r_y + |m_y| r_x + r_x r_y . \end{aligned}

With a rounded midpoint m=RN⁡p(mx∘my)m = \operatorname{RN}_p(m_x \circ m_y), the rounding error ∣m−mx∘my∣≤2−p∣m∣|m - m_x \circ m_y| \le 2^{-p}|m| is added, so

rx+y=RU⁡(rx+ry+2−p∣m∣),rxy=RU⁡(∣mx∣ry+∣my∣rx+rxry+2−p∣m∣).r_{x+y} = \operatorname{RU}\bigl(r_x + r_y + 2^{-p}|m|\bigr), \qquad r_{xy} = \operatorname{RU}\bigl(|m_x| r_y + |m_y| r_x + r_x r_y + 2^{-p}|m|\bigr).

These are cheap (the radius needs only a few bits), but the product radius overestimates: for x=y=⟨1,1⟩=[0,2]\boldsymbol{x} = \boldsymbol{y} = \langle 1, 1\rangle = [0, 2] it gives ⟨1,3⟩=[−2,4]\langle 1, 3\rangle = [-2, 4] while the exact product is [0,4][0, 4]. Rump showed that the width can grow by a factor up to 1.51.5 per multiplication compared to inf–sup.55 S. M. Rump, “Fast and parallel interval arithmetic”, BIT 39(3), 1999.

Options. (a) midpoint–radius storage with low-precision radii; (b) endpoint storage with full-precision endpoints; (c) both.

Chosen: (b). IEEE 1788 is defined on endpoints, half-unbounded and empty sets have no midpoint–radius form, and the hulls in the endpoint formulas are exact before rounding, so inf–sup results are as tight as the precision allows. The cost is that both endpoints carry pp bits, so wide intervals at high precision store many useless bits. The midpoint–radius view (from_midpoint_radius, midpoint, radius) is kept for users who reason in c±rc \pm r, with the conversions derived above.

Endpoint operations: one directed rounding of the exact result

Problem. Endpoint candidates can be computed with directed rounding at each step, or exactly and rounded once. Rounding at each step would not give the tightest enclosure; forming the exact value of a sum or a product by hand costs alignment and bookkeeping the binary layer already does.

Chosen. Every endpoint sum, difference and product of two Binary endpoints is one directed contextual operation of binary (add_ctx, sub_ctx, mul_ctx, fma_ctx) in a context of the precision pp of the result and the direction of the endpoint: lower endpoints toward −∞-\infty, upper endpoints toward +∞+\infty, radii and widths upward. Each of them computes as if its exact operands were summed or multiplied exactly and rounds that value once, so by property (iii) the result is the tightest pp-bit endpoint. The exponent range is the one of the library, so the three issues that an exact route had to handle itself belong to the binary layer:

  • Far addends. Adding A=2109A = 2^{10^9} and s=2−109s = 2^{-10^9} exactly aligns two coefficients over two billion bits (about 750 MB for one interval addition, issue #24). add_ctx does not align them: a term whose leading bit lies beyond the rounding position only decides the side of the rounding, and it is a sticky bit there. Soundness and tightness do not depend on a bound for the distance, as the replacement of the small addend by a surrogate that earlier versions made here did, and no precision label of an operand enters.
  • The edge of the exponent range. A result beyond the range is clamped by the rounding mode of the operation: a lower endpoint to −∞-\infty or the largest finite number, an upper endpoint to +∞+\infty or the largest finite number, a tiny result to zero or the smallest positive number. This keeps, for example, exp⁡([109,109])\exp([10^9, 10^9]) an enclosure [largest finite,+∞)[\text{largest finite}, +\infty) instead of collapsing it.
  • Gradual underflow. A result below the normal range lies on the grid of the format, and the directed rounding goes to that grid, not to the grid of the few bits the exact result happens to have.

The sign of a zero that is the result of an exact cancellation is the one of the direction (−0-0 toward −∞-\infty); it is not part of the interval (see the observers). Division and square root, whose exact results are not dyadic, are computed directly with directed rounding at precision pp. The midpoint needs the exact half-sum of the two bounds, not a rounded one: it adds the bounds rounded toward zero to p+65,538p + 65{,}538 bits and makes the result odd when that rounding was inexact (round to odd), so that the one rounding that follows, to at most pp bits in any direction, is the rounding of the exact value; an exponent gap of less than 65,536 places gives the exact center.

Contexts and double rounding

IntervalContext carries a target precision qq and exponent range; apply_ctx rounds endpoints outward into it and the *_ctx operations compute at the operands’ precision pp and then apply the context. Rounding twice is harmless for directed rounding when q≤pq \le p:

RD⁡q(RD⁡p(a))=RD⁡q(a)(q≤p),\operatorname{RD}_q(\operatorname{RD}_p(a)) = \operatorname{RD}_q(a) \quad (q \le p),

because Fq⊆FpF_q \subseteq F_p (a qq-bit significand is a pp-bit one padded with zeros): every f∈Fqf \in F_q below aa is in FpF_p, hence below RD⁡p(a)\operatorname{RD}_p(a), and conversely. So (x + y).apply_ctx(ctx) equals a single outward rounding of the exact sum into the context whenever the operands are at least as precise as the context. Overflow is resolved in the enclosing direction: an upper endpoint above the range becomes +∞+\infty, a positive lower endpoint becomes the largest finite number (still below the true value). Underflow rounds outward on the subnormal grid, so a tiny positive upper bound becomes the smallest subnormal and never 0. The flags are returned, never stored glointervaly.

Total and try_ forms

Problem. Certified evaluation can fail: the refinement budget may run out, or an argument may be too large to reduce. Aborting would break long computations; returning a silent wide interval hides a lost guarantee of tightness.

Chosen. Both, with one kernel. The try_ forms return an ArithmeticError with a certification detail (operation, stage, reason, precision). The total form is the try_ form with a fallback: when the try_ form fails it returns the range of the function over its domain, which keeps the inclusion property: [0,+∞)[0, +\infty) for exp, exp2, exp10, pow and hypot, [−1,+∞)[-1, +\infty) for expm1, Entire for the logarithms, sinh, asinh, atanh, tan, tanpi and log1p, [−1,1][-1, 1] for sin, cos, sinpi, cospi and tanh, [1,+∞)[1, +\infty) for cosh, [0,+∞)[0, +\infty) for acosh, [−π/2,π/2][-\pi/2, \pi/2] for atan and asin, [0,π][0, \pi] for acos, [−π,π][-\pi, \pi] for atan2. The π\pi of a fallback is evaluated at no more than 64 bits and rounded outward, so a fallback cannot fail at any precision. The domain is decided before the kernel runs, so a failure is always a certification failure and never a domain error: an argument outside the domain gives Empty, a wide interval means the kernel gave up. Trigonometric reduction is capped: when the larger endpoint magnitude is at least 2max⁡(65536,4p)+12^{\max(65536, 4p)+1}, reduction would need more than that many bits, so the try_ forms report a resource limit and the total forms return the fallback.

Why one kernel. Until this was settled the total forms of fifteen functions had a kernel of their own (exact-rational series), which aborted at a high precision (about 32,000 bits for ln, issue 449) and lost all relative accuracy for a tiny argument in the hyperbolic functions, while the try_ forms used the binary kernels and had neither limit. Keeping two kernels for the same function meant two sets of bugs, and results that depended on which form the caller chose.

Decorations in a separate type

Problem. Decorations cost a field and a minimum per operation, and most users do not need them.

Chosen. Interval is the bare set; DecoratedInterval wraps it with a decoration and the NaI state. Bare operations stay cheap and simple, and the decorated type cannot be mixed with bare values by accident.

Precision as a tag

Each interval carries a precision tag; binary operations use the larger tag. This keeps a sequence of operations at the precision of its most precise input without a context argument, which is what a set-valued type needs to compose with operators (x + y). An IntervalContext is used when a specific format must be imposed.

Correctness and invariants

Representation invariant. A non-empty Interval has non-NaN endpoints, x‾≤x‾\underline{x} \le \overline{x}, x‾≠+∞\underline{x} \ne +\infty, x‾≠−∞\overline{x} \ne -\infty and precision ≥1\ge 1; every construction path ends in store_interval, which checks this and aborts otherwise. Empty is a flag with endpoints (+∞,−∞)(+\infty, -\infty).

Inclusion. Each public operation FF of Interval satisfies f(x)⊆F(x)f(\boldsymbol{x}) \subseteq F(\boldsymbol{x}), by the endpoint formulas and Corollary 2 for arithmetic, by the critical-point analysis and certified endpoint enclosures for elementary functions, and by the fallbacks for uncertified cases. By the fundamental theorem, so does every composition.

Tightness. Basic arithmetic, sqr, pown, fma, abs, min, max, intersection, convex_hull of non-empty operands and sqrt return the outward rounding of the exact hull, so each endpoint is within one ulp of optimal. Trigonometric and arctangent endpoints are correctly directed roundings when certified; the other elementary functions are usually within a few ulps. Not tight: fallbacks; pown with n<−4096n < -4096 and 0 inside (Entire); with_precision and normalized round the endpoints outward, which is the tightest enclosure and the identity on an interval already representable at the target precision, so convex_hull with an Empty operand and the checked capabilities do not widen either.

Decorations. The decoration of a result is a true statement about the function evaluated, by the induction argument above.

Complexity. Arithmetic performs at most four endpoint products or two sums, O(M(p))O(M(p)) for pp-bit endpoints with multiplication cost M(p)M(p), plus alignment of at most about 65536+p65536 + p bits for sums. Elementary functions cost one or two directed evaluations of the binary kernels at precision pp (see binary for their cost), plus the index range for the trigonometric functions.

Evidence. Package tests check directed endpoints, the far-addend and exponent-range cases, decorations and relations; the pinned ITF1788 corpus runs 6,632 cases in strict mode (see conformance).

Known limitations

No limitation is open at present. Decorated rootn with a negative degree lowers the decoration to trv when 0 is in the argument, as the decoration rules of IEEE 1788-2015 11.6 require; library/src/interval/interval_decorated_wbtest.mbt checks degrees -1, -2 and -3.

Alternatives rejected

  • Midpoint–radius storage (Arb-style): cheaper radii, but overestimating products and no representation for half-unbounded sets; see above.
  • Hardware Double endpoints with switched rounding modes: fixed precision, process-global state, and rounding-mode control is not available on every MoonBit target.
  • Round to nearest and inflate by one ulp (“epsilon inflation”): simpler, but about one ulp wider per endpoint than directed rounding, and only valid for operations whose nearest-rounded result is known to be within one ulp, which excludes most elementary function kernels.
  • Aborting on uncertified elementary functions: a single hard argument would stop a whole computation; the try_ forms cover callers who need the failure.
  • Treating division by zero-containing intervals as an error: IEEE 1788 defines these results as sets (Entire, half-unbounded or empty), and extended division is what makes interval Newton methods work.

Boundaries

The package deliberately does not:

  • provide reverse versions of division and reciprocal (Table 10.1 does not require them: change z = x / y to x = y z and use mul_rev);
  • promise tight results: fallbacks and the dependency problem may widen enclosures arbitrarily;
  • define a total order on intervals, or treat Eq as set equality;
  • convert decimal data outward on its own: from_double and exact enclose binary values, and enclosing a decimal literal is the caller’s job (see the tutorial);
  • implement intervals over decimal endpoints, complex intervals, interval vectors or matrices, or Taylor models;
  • report status through global flags: flags come back from *_ctx calls and decorations travel with values.

Footnotes

  1. IEEE Std 1788-2015, IEEE Standard for Interval Arithmetic. The package follows its set-based flavor. ↩

  2. R. E. Moore, Interval Analysis, Prentice-Hall, 1966; and Moore, Kearfott, Cloud, Introduction to Interval Analysis, SIAM, 2009, Thm. 5.1. ↩

  3. A. Ziv, “Fast evaluation of elementary mathematical functions with correctly rounded last bit”, ACM TOMS 17(3), 1991; J.-M. Muller et al., Handbook of Floating-Point Arithmetic, 2nd ed., Birkhäuser, 2018, §10. ↩

  4. J. van der Hoeven, “Ball arithmetic”, 2009; F. Johansson, “Arb: efficient arbitrary-precision midpoint-radius interval arithmetic”, IEEE Trans. Computers 66(8), 2017. ↩

  5. S. M. Rump, “Fast and parallel interval arithmetic”, BIT 39(3), 1999. ↩