Skip to content

Commit 76d7f0c

Browse files
committed
Display true/false whether an interval is guaranteed in :full format
1 parent 6f6a877 commit 76d7f0c

File tree

7 files changed

+82
-52
lines changed

7 files changed

+82
-52
lines changed

src/display.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ end
88
function Base.show(io::IO, ::MIME"text/plain", params::DisplayOptions)
99
println(io, "Display options:")
1010
println(io, " - format: ", params.format)
11-
println(io, " - decorations: ", params.decorations)
12-
println(io, " - NG flag: ", params.ng_flag)
1311
if params.format === :full
12+
println(io, " - decorations: ", params.decorations, " (ignored)")
13+
println(io, " - NG flag: ", params.ng_flag, " (ignored)")
1414
print(io, " - significant digits: ", params.sigdigits, " (ignored)")
1515
else
16+
println(io, " - decorations: ", params.decorations)
17+
println(io, " - NG flag: ", params.ng_flag)
1618
print(io, " - significant digits: ", params.sigdigits)
1719
end
1820
end
@@ -54,12 +56,12 @@ julia> x = interval(0.1, 0.3)
5456
julia> setdisplay(:full)
5557
Display options:
5658
- format: full
57-
- decorations: true
58-
- NG flag: true
59+
- decorations: true (ignored)
60+
- NG flag: true (ignored)
5961
- significant digits: 6 (ignored)
6062
6163
julia> x
62-
Interval{Float64}(0.1, 0.3, com)
64+
Interval{Float64}(0.1, 0.3, com, true)
6365
6466
julia> setdisplay(:infsup; sigdigits = 3)
6567
Display options:
@@ -119,15 +121,15 @@ function _str_repr(a::Interval{T}, format::Symbol) where {T<:NumTypes}
119121
# `format` is either `:infsup`, `:midpoint` or `:full`
120122
str_interval = _str_basic_repr(a.bareinterval, format) # use `a.bareinterval` to not print a warning if `a` is an NaI
121123
if format === :full && str_interval != ""
122-
str_interval = string("Interval{", T, "}(", str_interval, ", ", decoration(a), ')')
124+
return string("Interval{", T, "}(", str_interval, ", ", decoration(a), ", ", isguaranteed(a), ')')
123125
else
124126
str_interval = _str_precision(str_interval, a, format)
125127
if format === :midpoint && str_interval != "" && T !== BigFloat && (display_options.decorations | (display_options.ng_flag & !isguaranteed(a)))
126128
str_interval = string('(', str_interval, ')')
127129
end
128130
str_interval = ifelse(display_options.decorations, string(str_interval, '_', decoration(a)), str_interval)
131+
return ifelse(display_options.ng_flag & !isguaranteed(a), string(str_interval, "_NG"), str_interval)
129132
end
130-
return ifelse(display_options.ng_flag & !isguaranteed(a), string(str_interval, "_NG"), str_interval)
131133
end
132134

133135
function _str_repr(x::Complex{Interval{T}}, format::Symbol) where {T<:NumTypes}

src/intervals/construction.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ See also: [`interval`](@ref), [`±`](@ref), [`..`](@ref) and [`@I_str`](@ref).
135135
# Examples
136136
137137
```jldoctest
138+
julia> using IntervalArithmetic
139+
138140
julia> setdisplay(:full);
139141
140142
julia> bareinterval(1//1, π)
@@ -318,6 +320,8 @@ In the case of a complex interval `x`, this is semantically equivalent to
318320
# Examples
319321
320322
```jldoctest
323+
julia> using IntervalArithmetic
324+
321325
julia> isguaranteed(bareinterval(1))
322326
true
323327
@@ -357,19 +361,21 @@ See also: [`±`](@ref), [`..`](@ref) and [`@I_str`](@ref).
357361
# Examples
358362
359363
```jldoctest
364+
julia> using IntervalArithmetic
365+
360366
julia> setdisplay(:full);
361367
362368
julia> interval(1//1, π)
363-
Interval{Rational{Int64}}(1//1, 85563208//27235615, com)
369+
Interval{Rational{Int64}}(1//1, 85563208//27235615, com, true)
364370
365371
julia> interval(Rational{Int32}, 1//1, π)
366-
Interval{Rational{Int32}}(1//1, 85563208//27235615, com)
372+
Interval{Rational{Int32}}(1//1, 85563208//27235615, com, true)
367373
368374
julia> interval(1, π)
369-
Interval{Float64}(1.0, 3.1415926535897936, com)
375+
Interval{Float64}(1.0, 3.1415926535897936, com, true)
370376
371377
julia> interval(BigFloat, 1, π)
372-
Interval{BigFloat}(1.0, 3.141592653589793238462643383279502884197169399375105820974944592307816406286233, com)
378+
Interval{BigFloat}(1.0, 3.141592653589793238462643383279502884197169399375105820974944592307816406286233, com, true)
373379
```
374380
"""
375381
function interval(::Type{T}, a, b, d::Decoration = com; format::Symbol = :infsup) where {T}
@@ -574,16 +580,18 @@ to `parse(Interval{T}, string(x))` if `x` is a `Number`.
574580
# Examples
575581
576582
```jldoctest
583+
julia> using IntervalArithmetic
584+
577585
julia> setdisplay(:full);
578586
579587
julia> x = IntervalArithmetic.atomic(Float64, 0.1)
580-
Interval{Float64}(0.09999999999999999, 0.1, com)
588+
Interval{Float64}(0.09999999999999999, 0.1, com, true)
581589
582590
julia> in_interval(1//10, IntervalArithmetic.atomic(Float64, 0.1))
583591
true
584592
585593
julia> IntervalArithmetic.atomic(Float64, 0.3)
586-
Interval{Float64}(0.3, 0.30000000000000004, com)
594+
Interval{Float64}(0.3, 0.30000000000000004, com, true)
587595
588596
julia> in_interval(3//10, IntervalArithmetic.atomic(Float64, 0.3))
589597
true
@@ -615,6 +623,8 @@ constructor [`atomic`](@ref).
615623
# Examples
616624
617625
```jldoctest
626+
julia> using IntervalArithmetic
627+
618628
julia> setdisplay(:full);
619629
620630
julia> @macroexpand @interval sin(1) # Float64 is the default bound type
@@ -624,7 +634,7 @@ julia> @macroexpand @interval Float32 sin(1)
624634
:(sin(IntervalArithmetic.atomic(Float32, 1)))
625635
626636
julia> @interval Float64 sin(1) exp(1)
627-
Interval{Float64}(0.8414709848078965, 2.7182818284590455, com)
637+
Interval{Float64}(0.8414709848078965, 2.7182818284590455, com, true)
628638
```
629639
"""
630640
macro interval(T, expr)

src/intervals/exact_literals.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,27 @@ producing the "NG" flag.
2626
# Examples
2727
2828
```jldoctest
29+
julia> using IntervalArithmetic
30+
2931
julia> setdisplay(:full);
3032
3133
julia> 0.5 * interval(1)
32-
Interval{Float64}(0.5, 0.5, com)_NG
34+
Interval{Float64}(0.5, 0.5, com, false)
3335
3436
julia> ExactReal(0.5) * interval(1)
35-
Interval{Float64}(0.5, 0.5, com)
37+
Interval{Float64}(0.5, 0.5, com, false)
38+
39+
julia> setdisplay(:infsup);
3640
3741
julia> [1, interval(2)]
3842
2-element Vector{Interval{Float64}}:
39-
Interval{Float64}(1.0, 1.0, com)_NG
40-
Interval{Float64}(2.0, 2.0, com)
43+
[1.0, 1.0]_com_NG
44+
[2.0, 2.0]_com
4145
4246
julia> [ExactReal(1), interval(2)]
4347
2-element Vector{Interval{Float64}}:
44-
Interval{Float64}(1.0, 1.0, com)
45-
Interval{Float64}(2.0, 2.0, com)
48+
[1.0, 1.0]_com
49+
[2.0, 2.0]_com
4650
```
4751
4852
See also: [`@exact`](@ref).
@@ -206,19 +210,21 @@ macro allows defining generic functions, seamlessly accepting both `Number` and
206210
# Examples
207211
208212
```jldoctest
209-
julia> setdisplay(:full);
213+
julia> using IntervalArithmetic
214+
215+
julia> setdisplay(:infsup);
210216
211-
julia> f(x) = 1.2*x + 0.1
217+
julia> f(x) = 1.2 * x + 0.1
212218
f (generic function with 1 method)
213219
214220
julia> f(interval(1, 2))
215-
Interval{Float64}(1.2999999999999998, 2.5, com)_NG
221+
[1.29999, 2.5]_com_NG
216222
217-
julia> @exact g(x) = 1.2*x + 0.1
223+
julia> @exact g(x) = 1.2 * x + 0.1
218224
g (generic function with 1 method)
219225
220226
julia> g(interval(1, 2))
221-
Interval{Float64}(1.2999999999999998, 2.5, com)
227+
[1.29999, 2.5]_com
222228
223229
julia> g(1.4)
224230
1.78

src/intervals/parsing.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ equivalent to `parse(Interval{[DEFAULT BOUND TYPE]}, "str")`.
77
# Examples
88
99
```jldoctest
10+
julia> using IntervalArithmetic
11+
1012
julia> setdisplay(:full);
1113
1214
julia> I"[3, 4]"
13-
Interval{Float64}(3.0, 4.0, com)
15+
Interval{Float64}(3.0, 4.0, com, true)
1416
1517
julia> I"0.1"
16-
Interval{Float64}(0.09999999999999999, 0.1, com)
18+
Interval{Float64}(0.09999999999999999, 0.1, com, true)
1719
1820
julia> in_interval(1//10, I"0.1")
1921
true
@@ -57,6 +59,8 @@ For more details, see sections 9.7 and 12.11 of the IEEE Standard 1788-2015.
5759
# Examples
5860
5961
```jldoctest
62+
julia> using IntervalArithmetic
63+
6064
julia> setdisplay(:full);
6165
6266
julia> parse(BareInterval{Float64}, "[1, 2]")
@@ -72,13 +76,13 @@ julia> parse(BareInterval{Float64}, "6.42?2e2")
7276
BareInterval{Float64}(640.0, 644.0)
7377
7478
julia> parse(Interval{Float64}, "[1, 2]")
75-
Interval{Float64}(1.0, 2.0, com)
79+
Interval{Float64}(1.0, 2.0, com, true)
7680
7781
julia> parse(Interval{Float64}, "[1, 2]_def")
78-
Interval{Float64}(1.0, 2.0, def)
82+
Interval{Float64}(1.0, 2.0, def, true)
7983
8084
julia> parse(Interval{Float64}, "[1, 1e400]_com")
81-
Interval{Float64}(1.0, Inf, dac)
85+
Interval{Float64}(1.0, Inf, dac, true)
8286
```
8387
"""
8488
function Base.parse(::Type{BareInterval{T}}, str::AbstractString) where {T<:NumTypes}

src/piecewise.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,18 @@ Return an interval containing only the value for an interval input,
103103
and the value directly otherwise.
104104
105105
```jldoctest
106+
julia> using IntervalArithmetic
107+
108+
julia> setdisplay(:full);
109+
106110
julia> c = Constant(1.2)
107111
Constant{Float64}(1.2)
108112
109113
julia> c(22.2)
110114
1.2
111115
112116
julia> c(interval(0, 1.3))
113-
Interval{Float64}(1.2, 1.2, com)
117+
Interval{Float64}(1.2, 1.2, com, true)
114118
```
115119
116120
Note that this is not equivalent to `Returns(value)` from base,
@@ -131,7 +135,11 @@ end
131135
A function defined by pieces (each associating a domain to a function).
132136
Support both intervals and standard numbers.
133137
134-
```jl
138+
```jldoctest
139+
julia> using IntervalArithmetic
140+
141+
julia> setdisplay(:full);
142+
135143
julia> myabs = Piecewise(
136144
Domain{:open, :closed}(-Inf, 0) => x -> -x,
137145
Domain{:open, :open}(0, Inf) => identity
@@ -144,7 +152,7 @@ julia> myabs(-22.3)
144152
22.3
145153
146154
julia> myabs(interval(-5, 5))
147-
Interval{Float64}(0.0, 5.0, def)
155+
Interval{Float64}(0.0, 5.0, def, true)
148156
```
149157
150158
For constant pieces, it is recommended to use `Constant`
@@ -203,7 +211,7 @@ function Piecewise(
203211
if length(domains) != length(fs)
204212
throw(ArgumentError("the number of domains and the number of functions don't match"))
205213
end
206-
214+
207215
if length(domains) - 1 != length(continuity)
208216
n = length(domains)
209217
throw(ArgumentError("$(length(sub)) junction points but $(n - 1) are expected based on the number of domains ($n)"))
@@ -251,7 +259,7 @@ end
251259
function Base.show(io::IO, ::MIME"text/plain", piecewise::Piecewise)
252260
n = length(pieces(piecewise))
253261
print(io, "Piecewise function with $n pieces:")
254-
262+
255263
for (domain, f) in pieces(piecewise)
256264
println(io)
257265
print(io, " $(domain_string(domain)) -> $(repr(f))")
@@ -292,7 +300,7 @@ function (piecewise::Piecewise)(X::Interval{T}) where {T}
292300
if !in_domain(input_domain, piecewise)
293301
dec = trv
294302
elseif any(x -> in_domain(x, input_domain), discontinuities(piecewise))
295-
dec = def
303+
dec = def
296304
else
297305
dec = com
298306
end
@@ -313,4 +321,4 @@ function (piecewise::Piecewise)(x::Real)
313321
(in_domain(x, domain)) && return f(x)
314322
end
315323
throw(DomainError(x, "piecewise function was called outside of its domain $(domain_string(piecewise))"))
316-
end
324+
end

src/symbols.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ julia> using IntervalArithmetic.Symbols
2727
julia> setdisplay(:full);
2828
2929
julia> (1//1)..π
30-
Interval{Rational{Int64}}(1//1, 85563208//27235615, com)
30+
Interval{Rational{Int64}}(1//1, 85563208//27235615, com, true)
3131
3232
julia> 0.1..0.3
33-
Interval{Float64}(0.1, 0.3, com)
33+
Interval{Float64}(0.1, 0.3, com, true)
3434
```
3535
"""
3636
..(a, b) = interval(a, b; format = :infsup)
@@ -60,10 +60,10 @@ julia> using IntervalArithmetic.Symbols
6060
julia> setdisplay(:full);
6161
6262
julia> 0 ± π
63-
Interval{Float64}(-3.1415926535897936, 3.1415926535897936, com)
63+
Interval{Float64}(-3.1415926535897936, 3.1415926535897936, com, true)
6464
6565
julia> 0//1 ± π
66-
Interval{Rational{Int64}}(-85563208//27235615, 85563208//27235615, com)
66+
Interval{Rational{Int64}}(-85563208//27235615, 85563208//27235615, com, true)
6767
```
6868
"""
6969
±(m, r) = interval(m, r; format = :midpoint)

test/interval_tests/display.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ setprecision(BigFloat, 256) do
125125
@test sprint(show, MIME("text/plain"), emptyinterval()) == ""
126126
@test sprint(show, MIME("text/plain"), emptyinterval()/1) == "∅_NG"
127127

128-
@test sprint(show, MIME("text/plain"), a) == "Interval{Float64}(1.0, 2.0, com)"
129-
@test sprint(show, MIME("text/plain"), a_NG) == "Interval{Float64}(1.0, 2.0, com)_NG"
130-
@test sprint(show, MIME("text/plain"), b) == "Interval{Float64}(-2.2250738585072014e-308, 1.3, com)"
131-
@test sprint(show, MIME("text/plain"), b32) == "Interval{Float32}(-1.1754944f-38, 1.3f0, com)"
132-
@test sprint(show, MIME("text/plain"), b16) == "Interval{Float16}(Float16(-6.104e-5), Float16(1.3), com)"
133-
@test sprint(show, MIME("text/plain"), br) == "Interval{Rational{Int64}}(-11//10, 13//10, com)"
134-
@test sprint(show, MIME("text/plain"), c) == "Interval{Float64}(-1.0, Inf, dac)"
128+
@test sprint(show, MIME("text/plain"), a) == "Interval{Float64}(1.0, 2.0, com, true)"
129+
@test sprint(show, MIME("text/plain"), a_NG) == "Interval{Float64}(1.0, 2.0, com, false)"
130+
@test sprint(show, MIME("text/plain"), b) == "Interval{Float64}(-2.2250738585072014e-308, 1.3, com, true)"
131+
@test sprint(show, MIME("text/plain"), b32) == "Interval{Float32}(-1.1754944f-38, 1.3f0, com, true)"
132+
@test sprint(show, MIME("text/plain"), b16) == "Interval{Float16}(Float16(-6.104e-5), Float16(1.3), com, true)"
133+
@test sprint(show, MIME("text/plain"), br) == "Interval{Rational{Int64}}(-11//10, 13//10, com, true)"
134+
@test sprint(show, MIME("text/plain"), c) == "Interval{Float64}(-1.0, Inf, dac, true)"
135135
@test sprint(show, MIME("text/plain"), large_expo) ==
136-
"Interval{BigFloat}(0.0, $(sup(large_expo)), com)"
136+
"Interval{BigFloat}(0.0, $(sup(large_expo)), com, true)"
137137
end
138138

139139
@testset "Midpoint format" begin
@@ -208,9 +208,9 @@ setprecision(BigFloat, 256) do
208208
# `sigdigits` and `decorations` keywords are not taken into account for format `:full`
209209
setdisplay(:full; sigdigits = 100, decorations = false)
210210

211-
@test sprint(show, MIME("text/plain"), a) == "Interval{Float64}(0.0, 2.0, com) + im*Interval{Float64}(1.0, 1.0, com)"
212-
@test sprint(show, MIME("text/plain"), b) == "Interval{Float64}(0.0, 2.0, com) - im*Interval{Float64}(1.0, 1.0, com)"
213-
@test sprint(show, MIME("text/plain"), c) == "Interval{Float64}(0.0, 1.0e-70, com) - im*Interval{Float64}(1.0e-70, 1.0e-70, com)"
211+
@test sprint(show, MIME("text/plain"), a) == "Interval{Float64}(0.0, 2.0, com, true) + im*Interval{Float64}(1.0, 1.0, com, true)"
212+
@test sprint(show, MIME("text/plain"), b) == "Interval{Float64}(0.0, 2.0, com, true) - im*Interval{Float64}(1.0, 1.0, com, true)"
213+
@test sprint(show, MIME("text/plain"), c) == "Interval{Float64}(0.0, 1.0e-70, com, true) - im*Interval{Float64}(1.0e-70, 1.0e-70, com, true)"
214214
end
215215

216216
@testset "Midpoint format" begin

0 commit comments

Comments
 (0)