|
| 1 | +# Constructing intervals |
| 2 | + |
| 3 | +Constructing an interval is the most basic operation in the library. There are several methods to construct intervals listed below. |
| 4 | + |
| 5 | +Note that a valid interval `[a, b]` must have `a ≤ b`. |
| 6 | + |
| 7 | +- `interval(x)` |
| 8 | + |
| 9 | + `interval(x, y)` |
| 10 | + |
| 11 | + This is the most fundamental way to build an interval for a user. It accepts one or two floating-point values and constructs the resulting interval with lower and upper endpoints exactly equal to those floating-point values, checking that the resulting interval is valid: |
| 12 | + |
| 13 | + ``` |
| 14 | +
|
| 15 | + julia> using IntervalArithmetic |
| 16 | +
|
| 17 | + julia> @format full # print out literal interval values in full |
| 18 | +
|
| 19 | + julia> interval(0.1) |
| 20 | + Interval(0.1, 0.1) |
| 21 | +
|
| 22 | + julia> interval(0.1, 0.2) |
| 23 | + Interval(0.1, 0.2) |
| 24 | +
|
| 25 | + julia> interval(3.1f0) |
| 26 | + Interval(3.1, 3.1) |
| 27 | +
|
| 28 | + julia> typeof(ans) |
| 29 | + IntervalArithmetic.Interval{Float32} |
| 30 | +
|
| 31 | + julia> interval(Inf) |
| 32 | + ERROR: ArgumentError: `[Inf, Inf]` is not a valid interval. Need `a ≤ b` to construct `interval(a, b)`. |
| 33 | +
|
| 34 | + julia> interval(3, 2) |
| 35 | + ERROR: ArgumentError: `[3, 2]` is not a valid interval. Need `a ≤ b` to construct `interval(a, b)`. |
| 36 | + ``` |
| 37 | + |
| 38 | + Note that `interval` *does not perform any rounding of the end-points*. E.g. |
| 39 | + ``` |
| 40 | + julia> x = interval(0.1) |
| 41 | + Interval(0.1, 0.1) |
| 42 | +
|
| 43 | + julia> big(x) |
| 44 | + Interval(1.000000000000000055511151231257827021181583404541015625000000000000000000000000e-01, 1.000000000000000055511151231257827021181583404541015625000000000000000000000000e-01) |
| 45 | +
|
| 46 | + julia> big"0.1" ∈ x |
| 47 | + false |
| 48 | + ``` |
| 49 | + See [here](rounding.md) for more on the need for rounding. |
| 50 | + |
| 51 | + |
| 52 | +- `x..y` |
| 53 | + |
| 54 | + This is a convenient syntax, and tries to be "clever" by interpreting the values as user-friendly numbers, rather than strict floating-point, and performing [directed rounding](rounding.md) automatically to give an interval that is guaranteed to contain the corresponding true real numbers. For example: |
| 55 | + |
| 56 | + ``` |
| 57 | + julia> 0.1..0.2 |
| 58 | + Interval(0.09999999999999999, 0.2) |
| 59 | +
|
| 60 | + julia> big(ans) |
| 61 | + Interval(9.999999999999999167332731531132594682276248931884765625000000000000000000000000e-02, 2.000000000000000111022302462515654042363166809082031250000000000000000000000000e-01) |
| 62 | + ``` |
| 63 | + |
| 64 | + So `0.1..0.2` contains both the true real number `1/10` and `2/10`. |
| 65 | + |
| 66 | + To do so, floating-point values like `0.1` are treated as the smallest interval containing the true real number 1/10, given by the unexported `atomic` function: |
| 67 | + |
| 68 | + ``` |
| 69 | + julia> IntervalArithmetic.atomic(Interval{Float64}, 0.1) |
| 70 | + Interval(0.09999999999999999, 0.1) |
| 71 | +
|
| 72 | + julia> 0.1..0.1 |
| 73 | + Interval(0.09999999999999999, 0.1) |
| 74 | + ``` |
| 75 | + |
| 76 | +- `m ± r` |
| 77 | + |
| 78 | + The `±` operator (typed as `\pm<TAB>`) creates the interval with midpoint `m` and radius `r`, and is equivalent to |
| 79 | + `(m - r) .. (m + r)`: |
| 80 | + |
| 81 | + ``` |
| 82 | + julia> 1 ± 0.1 |
| 83 | + Interval(0.8999999999999999, 1.1) |
| 84 | + ``` |
| 85 | + |
| 86 | +- `@interval expr` |
| 87 | + |
| 88 | + The `@interval` macro takes a Julia expression and calculates an interval that is guaranteed to contain the true result of the calculation, treating literals in the same way as the `..` operator, e.g. |
| 89 | + |
| 90 | + ``` |
| 91 | + julia> x = @interval sin(0.1) + cos(0.2) |
| 92 | + Interval(1.0798999944880696, 1.07989999448807) |
| 93 | +
|
| 94 | + julia> sin(big"0.1") + cos(big"0.2") ∈ x |
| 95 | + true |
| 96 | + ``` |
| 97 | + |
| 98 | + |
| 99 | +- `Interval(x)` |
| 100 | + |
| 101 | + `Interval(x1, x2)` |
| 102 | + |
| 103 | + `Interval` is the underlying interval constructor. Since v0.12 of the package, however, *for efficiency reasons this performs no tests on the validity of the interval, and allows invalid intervals to be created*. As a result, we recommend that *this should not be used in user code*; it should only be used in library functions which guarantee that the interval is already of the correct form. |
| 104 | + |
| 105 | + For example, the following creates an invalid interval which will cause problems later: |
| 106 | + |
| 107 | + ``` |
| 108 | + julia> Interval(3, 2) # do *not* do this |
| 109 | + [3, 2] |
| 110 | + ``` |
0 commit comments