ball_float design
ball_float 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. ball_float 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:
- 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.
- Explicit semantics. Precision, target format, flags and decorations are values passed in and returned, never process-global state (no hardware rounding-mode switching).
- 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 with , and , or the empty set . 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 is called Entire.
For a real function with domain , the range over a box is
and its hull is the smallest interval containing it. An interval extension of is a map with for every box. Points outside are ignored rather than reported as errors: and . Whether a domain violation occurred is reported separately, by decorations.
Every public operation of BallFloat is an interval extension. 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 evaluates to . For , let be a point where is defined; then each is defined at and, by the induction hypothesis, , the interval value of . Since is defined at and extends ,
The full argument, with the lemmas below, is in the attachment:
Outward rounding
Endpoints are BinFloat numbers with significant bits, so exact endpoint
values must be rounded. Write for the -bit binary numbers and
Three properties follow directly from these definitions:
(ii) holds because the set maximized for contains the one for ; (iii) follows from (ii), since the minimum over is attained at . Hence, if and for a set ,
This is the only way rounding enters the package: each operation computes a
lower bound and an upper bound of the exact range and stores
and . 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 a sum with exact range :
using and . Widths therefore grow additively through a computation, plus a relative per rounding.
Endpoint formulas
BallFloat stores the two endpoints (see
Endpoints, not midpoint and radius), so
each operation is a formula in the endpoints.
Sum and difference. is increasing in both arguments and is increasing in and decreasing in , so the extremes over the box are at the corners that make each argument extreme in the right direction:
Product. For fixed , is affine, so its extrema over lie at or ; applying the same argument to gives
The sign pattern decides which elements of can be extremal. If
the product is increasing in both
arguments, so the range is ; the other single-sign cases follow from
, which gives the table used by
multiplication_bounds:
| lower | upper | ||
|---|---|---|---|
In the last row both intervals straddle 0, so and are and the other two are ; the minimum is among the former and the maximum among the latter. For unbounded intervals all four products are formed with : real points near a zero endpoint give products near 0, whatever the other factor.
Division. IEEE 1788 defines . When , is continuous and decreasing on , so and ; the code evaluates the selected endpoint quotients directly with directed division instead of rounding twice. When :
- : no admissible , the result is .
- , : approaches 0 from both sides, so is unbounded in both directions: Entire.
- with and : as , , and the smallest quotient is , so the result is ; the other sign cases are mirror images. If straddles 0, both signs are unbounded: Entire.
- and : every admissible quotient is 0.
From midpoint–radius to endpoints
Users often know a value as . BallFloat::new(c, r, precision=p)
rounds the center to nearest, , and stores
Claim: . For ,
The endpoints are then formed exactly (they may have more
than bits). with_precision uses the same construction with the current
center and radius, and a caller-chosen rounding mode for the center. The
converse view is exact: center returns
and radius returns , which are dyadic and
need no rounding (the radius is rounded up only if it underflows the
exponent range), so is exactly the stored interval. Show prints this pair.
The dependency problem
The fundamental theorem treats every occurrence of a variable as an independent point. For ,
The result is correct (it contains 0) but not tight: interval subtraction is
the extension of , and the box
contains and . In
general with equality only
when each variable occurs once in the expression (a second theorem of
Moore’s). The same effect explains for
( but ) and
subdistributivity, . For
this reason the package provides single-occurrence operations — square,
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
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 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 decreasingacos): the range over is (swapped for decreasing ), 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 ( as ). -
Functions with known extrema.
coshhas its minimum 1 at 0.pownwith even exponent has minimum 0 at 0.sinattains at with and at ;cosattains at and at . Between two consecutive critical points both functions are monotone, so withThe code encloses by , where are computed from a certified enclosure of . This set can only be too large: a spurious critical point widens the result to , a missed one would break inclusion. When it has four or more elements every residue occurs.
sinpi,cospiandtanpiuse the exact critical points , computed from the dyadic endpoints without any approximation of . -
Poles. is continuous and increasing between consecutive poles . If contains an odd index the interval may contain a pole and the result is Entire; otherwise it is . 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_interval(x, y) on the domain (and , ) uses
the fact that is monotone in each argument for a fixed sign of
and of , so its extrema over a box are among the four corners,
plus the value 1 when the box crosses or (where the
monotonicity direction changes), and the limits 0 and when
. atan2 is handled the same way with the axis crossings as
additional candidates, and with the result when the box crosses
the branch cut on the negative axis.
Certified evaluation of transcendental endpoints
is not a dyadic number, so an endpoint needs a rational
enclosure of the exact value, after which
is a valid lower endpoint by property (i). The
kernels of this package build and with exact rational arithmetic and
directed BinFloat operations at a work precision :
- exp. For the argument is halved times so
that , the Taylor series is summed with directed
rounding, and the result is squared times in interval arithmetic,
. Each squaring doubles the relative width, so
the work precision is . The series tail after the term
is bounded using :
Negative arguments use . For ,
for the whole
BinFloatexponent range, and the result is or directly. - ln. For with , , and with ; is the same series at . Successive terms shrink by at least , so the omitted tail after doubling is at most for the first omitted term .
- π. Machin’s formula with alternating series, whose truncation error is bounded by the first omitted term.
- sin, cos. The argument is reduced by the quadrant
, computed with both and ;
if the two disagree the work precision is raised. The reduced argument
(as a rational interval) is fed to
Taylor series, and the quadrant maps to
. The initial work precision is plus the number
of integer bits of , so that reducing a huge argument keeps enough
bits.33 Payne–Hanek reduction would avoid the extra bits; this package
instead raises the work precision and caps it with the resource cutoff
described under the total and
try_forms.
The trigonometric and arctangent kernels add a Ziv-style acceptance test.44 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. Since is monotone,
so when both and of and agree,
the endpoints are the correctly directed roundings of . Otherwise the
work precision grows to , at most 12 times
(CertifiedRefinementBudget). The exp and log kernels skip the test and keep
, , which are always valid
and, with 64 guard bits, almost always tight. The try_ forms, and the total
forms of expm1, log1p, sinpi, cospi, tanpi, pow_interval,
hypot and atan2 (which try them first), delegate the endpoints to the certified try_*_ctx functions of
bin_float, evaluated in unbounded contexts rounded toward
and . The total hyperbolic functions and asin/acos
evaluate their defining formulas in interval arithmetic at 64 to 192 extra
bits, which is valid by the fundamental theorem.
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 on a box :
| Decoration | Property |
|---|---|
com | , continuous on , and the result is bounded |
dac | and is continuous |
def | |
trv | always true |
ill | the value is NaI, not an interval |
The decorations are totally ordered by strength, , because each property implies the next. A decorated operation applied to decorated inputs returns
Why the minimum is sound. Suppose was produced by
on with property , and has on the box
of the . If , each is defined on
with values in (inclusion property), and
is defined on those values, so is defined on
. If , 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 with decoration at least
dac, the function is continuous on and maps it into itself,
so Brouwer’s theorem gives a fixed point in . Without the
decoration, would wrongly suggest that
is defined on .
The package computes from the operands by the domain tests listed in the
API reference: division
by an interval containing 0, a logarithm reaching , sqrt below 0
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
(nothing can be claimed about on an empty set’s preimage), 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 , which is a valid set.
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:
For the first line, "" is . For "": if and are
finite they are elements, so ; if
or , a large or a
small violates , 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: definitely_lt(x, y) is
false exactly when some pair satisfies . With empty operands the
universal statements would be vacuously true, which would let a caller prove
anything from an empty enclosure; the definitely_* relations therefore
return false for empty operands, whereas the IEEE 1788 relation precedes
() is defined to be vacuously true.
The set relations (subset, interior, disjoint, set_equal) and the
IEEE 1788 orders (less: and
, 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
, matching the enclosure reading of
the arithmetic traits.
Design decisions
Endpoints, not midpoint and radius
Problem. A ball can be stored as endpoints (inf–sup) or as a midpoint and radius , as in Arb.55 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 , and likewise for :
With a rounded midpoint , the rounding error is added, so
These are cheap (the radius needs only a few bits), but the product radius overestimates: for it gives while the exact product is . Rump showed that the width can grow by a factor up to per multiplication compared to inf–sup.66 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 bits, so wide intervals at high precision store many
useless bits. The midpoint–radius view (new, center, radius,
with_precision) is kept for users who reason in , with exact
conversions derived above.
Exact candidates, one directed rounding
Problem. Endpoint candidates can be computed with directed rounding at each step, or exactly and rounded once.
Chosen. Sums and products of BinFloat endpoints are formed exactly (the
coefficient grows to hold them), and quantize_interval applies one
/ at the end; by property (iii) the
result is the tightest -bit enclosure of the exact hull. Division and
square root, whose exact results are not dyadic, are computed directly with
directed rounding at precision . The rule needs two safeguards, described
next: bounded alignment of far-apart addends and outward clamping at the edge
of the exponent range.
Far addends: bound endpoint sums by precision
Problem. Adding and exactly aligns the two coefficients and builds a two-billion-bit number (about 750 MB for one interval addition, issue #24).
Chosen. Let be the exponent of the leading bit of , the exponent of the last bit of the larger addend , and . If , the small addend is replaced by a sticky surrogate when pushes the sum in the rounding direction of the endpoint being computed, and by otherwise. Then:
- Soundness, for every precision. . An upper endpoint with gets ; with it gets . Lower endpoints are symmetric. So the directed sum stays on the required side of the exact sum, which is all the inclusion property needs.
- No loss of tightness when . The -bit numbers near are multiples of , and , so no -bit number lies strictly between and ; both and fall in the same gap and round to the same endpoint (Lemma 7 of the attachment).
The alignment now costs at most about bits beyond the width of ,
independent of the exponent gap. The surrogate is also used for the
nearest-rounded sums inside center, where it is not exact; this is the
source of the limitation noted in Correctness.
Outward clamping at the exponent range
BinFloat has a finite (very wide) exponent range. An exact candidate beyond
it cannot be stored, so the exact helpers take the direction of the endpoint
being built: lower endpoints clamp toward , upper endpoints toward
, radii round up, and exponent sums are computed in 64 bits and
saturated. This keeps, for example, an enclosure
instead of collapsing it.
Contexts and double rounding
BallContext carries a target precision and exponent range;
apply_ctx rounds endpoints outward into it and the *_ctx operations
compute at the operands’ precision and then apply the context. Rounding
twice is harmless for directed rounding when :
because (a -bit significand is a -bit one padded with
zeros): every below is in , hence below
, and conversely. So x.add_ctx(y, 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 , 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 globally.
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. The total forms return a valid fallback enclosure
( for sin/cos, Entire for tan, for atan2,
the composed formula for pow, hypot and rootn, the range bounds
for expm1), which keeps the inclusion property; the try_
forms return an ArithmeticError with a certification detail (operation,
stage, reason, precision). Trigonometric reduction is capped: when the larger
endpoint magnitude is at least , reduction would need
more than that many bits, so the total forms return the fallback immediately
and the try_ forms report a resource limit.
Decorations in a separate type
Problem. Decorations cost a field and a minimum per operation, and most users do not need them.
Chosen. BallFloat is the bare set; BallFloatDecorated 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). A BallContext is used when a specific
format must be imposed.
Correctness / invariants
Representation invariant. A non-empty BallFloat has non-NaN endpoints,
, ,
and precision ; every construction path
ends in store_interval, which checks this and aborts otherwise. Empty is a
flag with endpoints .
Inclusion. Each public operation of BallFloat satisfies
, 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 (the exceptions are listed under known limitations). By the
fundamental theorem, so does every composition.
Tightness. Basic arithmetic, square, pown, fma, abs, minimum,
maximum, set operations and sqrt_interval 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;
other elementary functions are within a few ulps; fallbacks are not tight.
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, for -bit endpoints with multiplication cost , plus alignment of at most about bits for sums. Elementary functions sum series of terms at work precision (plus the integer bits of the argument for trigonometric reduction), with up to 12 refinements growing geometrically.
Evidence. Package tests check directed endpoints, the far-addend and exponent-range cases, decorations and relations; the pinned ITF1788 corpus runs 4,656 cases in strict mode (see conformance).
Known limitations
The following inputs currently break the inclusion property or the decoration rule; they are reported for fixing and documented here so that callers can avoid them.
from_int(n, precision=p)andfrom_coefficientround the integer to nearest at bits before building the singleton, sofrom_int(257, precision=8)is .with_precision(and thereforenormalized) rebuilds a bounded interval fromcenter(), which uses the far-addend surrogate with round-to-nearest. For endpoints more than about binary orders of magnitude apart and a new precision large enough to store the surrogate exactly (more than about 65536 bits), the result can lose the smaller endpoint.- Decorated
rootnwith a negative degree does not lower the decoration totrvwhen 0 is in the argument. midpoint_ctxdoes not apply the context’s and never raisesoverflow.
Alternatives rejected
- Midpoint–radius storage (Arb-style): cheaper radii, but overestimating products and no representation for half-unbounded sets; see above.
- Hardware
Doubleendpoints 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 the IEEE 1788 reverse operations (
sqrRev,mulRevToPair, …) or two-output division; - promise tight results: fallbacks and the dependency problem may widen enclosures arbitrarily;
- define a total order on intervals, or treat
Eqas set equality; - convert decimal data outward on its own:
from_doubleandexactenclose binary values, and enclosing a decimal literal is the caller’s job (see the tutorial); - implement intervals over decimal endpoints, complex balls, interval vectors or matrices, or Taylor models;
- report status through global flags: flags come back from
*_ctxcalls and decorations travel with values.
Footnotes
-
IEEE Std 1788-2015, IEEE Standard for Interval Arithmetic. The package follows its set-based flavor. ↩
-
R. E. Moore, Interval Analysis, Prentice-Hall, 1966; and Moore, Kearfott, Cloud, Introduction to Interval Analysis, SIAM, 2009, Thm. 5.1. ↩
-
Payne–Hanek reduction would avoid the extra bits; this package instead raises the work precision and caps it with the resource cutoff described under the total and
try_forms. ↩ -
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. ↩
-
J. van der Hoeven, “Ball arithmetic”, 2009; F. Johansson, “Arb: efficient arbitrary-precision midpoint-radius interval arithmetic”, IEEE Trans. Computers 66(8), 2017. ↩
-
S. M. Rump, “Fast and parallel interval arithmetic”, BIT 39(3), 1999. ↩