Skip to content

Commit 4199f4c

Browse files
authored
Allow changing decoration (#632)
* Allow changing decoration * Add details in docstring
1 parent 02b0f6b commit 4199f4c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/intervals/construction.jl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ end
236236
# note: `isless`, and hence `<`, `min` and `max`, are automatically defined
237237

238238
function decoration(x::BareInterval)
239-
isnai(x) && return ill
240239
isempty_interval(x) && return trv
241240
isunbounded(x) && return dac
242241
return com
@@ -302,6 +301,31 @@ end
302301
bareinterval(::Type{T}, x::Interval) where {T} = bareinterval(T, bareinterval(x))
303302
decoration(x::Interval) = x.decoration
304303

304+
"""
305+
setdecoration(x::Interval, d::Decoration)
306+
307+
Return the interval `bareinterval(x)` with decoration `d`, with the following
308+
exceptions:
309+
- if `d == ill`, then the output is an NaI
310+
- if `isempty_interval(bareinterval(x))` and `d != ill`, then the output has
311+
decoration `trv`
312+
- if `isunbounded(bareinterval(x))` and `d == com`, then the output has
313+
decoration `dac`
314+
315+
!!! danger
316+
Since misuse of this function can deeply corrupt code, its usage is
317+
**strongly discouraged**.
318+
319+
Implement the `setDec` function of the IEEE Standard 1788-2015 (Section 11.5.2).
320+
"""
321+
function setdecoration(x::Interval{T}, d::Decoration) where {T<:NumTypes}
322+
d == ill && return nai(T)
323+
bx = bareinterval(x)
324+
isempty_interval(bx) && return _unsafe_interval(bx, trv, isguaranteed(x))
325+
(isunbounded(bx) & (d == com)) && return _unsafe_interval(bx, dac, isguaranteed(x))
326+
return _unsafe_interval(bx, d, isguaranteed(x))
327+
end
328+
305329
"""
306330
isguaranteed(x::BareInterval)
307331
isguaranteed(x::Interval)

test/interval_tests/consistency.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
@test isequal_interval(interval(0, 1) - emptyinterval(a), emptyinterval(a))
3535
@test isequal_interval(interval(0, 1) * emptyinterval(a), emptyinterval(a))
3636
@test isequal_interval(a * interval(0), zero(a))
37+
38+
@test decoration(IntervalArithmetic.setdecoration(interval(1, 2), ill)) == ill
39+
@test decoration(IntervalArithmetic.setdecoration(emptyinterval(), com)) == trv
40+
@test decoration(IntervalArithmetic.setdecoration(interval(1, Inf), com)) == dac
41+
42+
@test !isnai(IntervalArithmetic.setdecoration(interval(NaN), com))
43+
@test decoration(IntervalArithmetic.setdecoration(interval(NaN), com)) == trv
3744
end
3845

3946
@testset "real interface" begin

0 commit comments

Comments
 (0)