Skip to content

Commit 69cd825

Browse files
committed
return empty interval for invalid input
1 parent 65ca77f commit 69cd825

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

src/intervals/intervals.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ function is_valid_interval(a::Real, b::Real)
7979
# println("isvalid()")
8080

8181
if isnan(a) || isnan(b)
82-
if isnan(a) && isnan(b)
83-
return true
84-
else
85-
return false
86-
end
82+
return false
8783
end
8884

8985
if a > b
@@ -106,9 +102,10 @@ end
106102
107103
`interval(a, b)` checks whether [a, b] is a valid `Interval`, which is the case if `-∞ <= a <= b <= ∞`, using the (non-exported) `is_valid_interval` function. If so, then an `Interval(a, b)` object is returned; if not, then an error is thrown.
108104
"""
109-
function interval(a::Real, b::Real)
105+
function interval(a::T, b::S) where {T<:Real, S<:Real}
110106
if !is_valid_interval(a, b)
111-
throw(ArgumentError("`[$a, $b]` is not a valid interval. Need `a ≤ b` to construct `interval(a, b)`."))
107+
@warn "Invalid input, empty interval is returned"
108+
return emptyinterval(promote_type(T, S, Float64))
112109
end
113110

114111
return Interval(a, b)

test/interval_tests/consistency.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ setprecision(Interval, Float64)
380380
@test interval(1, 2) == Interval(1, 2)
381381

382382
@test inf(Interval(3, 2)) == 3
383-
@test_throws ArgumentError interval(3, 2)
383+
@test isempty(interval(3, 2))
384384

385385
@test sup(Interval(Inf, Inf)) == Inf
386-
@test_throws ArgumentError interval(Inf, Inf)
386+
@test isempty(interval(Inf, Inf))
387387

388388
end
389389

test/interval_tests/construction.jl

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,20 @@ const eeuler = Base.MathConstants.e
6969

7070
# Disallowed conversions with a > b
7171

72-
@test_throws ArgumentError interval(2, 1)
73-
@test_throws ArgumentError interval(big(2), big(1))
74-
@test_throws ArgumentError interval(BigInt(1), 1//10)
75-
@test_throws ArgumentError interval(1, 0.1)
76-
@test_throws ArgumentError interval(big(1), big(0.1))
77-
78-
@test_throws ArgumentError @interval(2, 1)
79-
@test_throws ArgumentError @interval(big(2), big(1))
80-
@test_throws ArgumentError @interval(big(1), 1//10)
81-
@test_throws ArgumentError @interval(1, 0.1)
82-
@test_throws ArgumentError @interval(big(1), big(0.1))
83-
@test_throws ArgumentError interval(Inf)
84-
@test_throws ArgumentError interval(-Inf, -Inf)
85-
@test_throws ArgumentError interval(Inf, Inf)
72+
@test isempty(interval(2, 1))
73+
@test isempty(interval(big(2), big(1)))
74+
@test isempty(interval(BigInt(1), 1//10))
75+
@test isempty(interval(1, 0.1))
76+
@test isempty(interval(big(1), big(0.1)))
77+
78+
@test isempty(@interval(2, 1))
79+
@test isempty(@interval(big(2), big(1)))
80+
@test isempty(@interval(big(1), 1//10))
81+
@test isempty(@interval(1, 0.1))
82+
@test isempty(@interval(big(1), big(0.1)))
83+
@test isempty(interval(Inf))
84+
@test isempty(interval(-Inf, -Inf))
85+
@test isempty(interval(Inf, Inf))
8686

8787
# Conversion to Interval without type
8888
@test convert(Interval, 1) == Interval(1.0)
@@ -237,10 +237,10 @@ end
237237
a = big(0.1)..2
238238
@test typeof(a) == Interval{BigFloat}
239239

240-
@test_throws ArgumentError 2..1
241-
@test_throws ArgumentError π..1
242-
@test_throws ArgumentError π..eeuler
243-
@test_throws ArgumentError 4..π
240+
@test isempty(2..1)
241+
@test isempty(π..1)
242+
@test isempty(π..eeuler)
243+
@test isempty(4..π)
244244
@test 1..π == Interval(1, π)
245245
end
246246

@@ -292,9 +292,10 @@ end
292292
end
293293

294294
# issue 192:
295-
@testset "Disallow a single NaN in an interval" begin
296-
@test_throws ArgumentError interval(NaN, 2)
297-
@test_throws ArgumentError interval(Inf, NaN)
295+
@testset "Disallow NaN in an interval" begin
296+
@test isempty(interval(NaN, 2))
297+
@test isempty(interval(Inf, NaN))
298+
@test isempty(interval(NaN, NaN))
298299
end
299300

300301
# issue 206:
@@ -342,7 +343,7 @@ end
342343
end
343344

344345
@testset "a..b with a > b" begin
345-
@test_throws ArgumentError 3..2
346+
@test isempty(3..2)
346347
end
347348

348349
@testset "Hashing of Intervals" begin
@@ -374,7 +375,7 @@ import IntervalArithmetic: force_interval
374375
@test force_interval(4, Inf) == Interval(4, Inf)
375376
@test force_interval(Inf, 4) == Interval(4, Inf)
376377
@test force_interval(Inf, -Inf) == Interval(-Inf, Inf)
377-
@test_throws ArgumentError force_interval(NaN, 3)
378+
@test isempty(force_interval(NaN, 3))
378379
end
379380

380381
@testset "Zero interval" begin

test/interval_tests/numeric.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ setprecision(Interval, Float64)
1111
y = 4..5
1212
a = 3
1313
b = 12
14-
14+
1515
@test sqrt(sum(x.^2 .+ y.^2)) == 5..13
1616

1717
for i in 1:20
@@ -24,7 +24,7 @@ setprecision(Interval, Float64)
2424
end
2525

2626
a = 4
27-
b = 5
27+
b = 5
2828
for i in 1:20
2929
@test y.+i == (a+i)..(b+i)
3030
end
@@ -115,7 +115,7 @@ end
115115
@test Interval(1,2) ^ -3 == Interval(1/8, 1.0)
116116
@test Interval(0,3) ^ -3 == @interval(1/27, Inf)
117117
@test Interval(-1,2) ^ -3 == entireinterval()
118-
@test_throws ArgumentError interval(-1, -2) ^ -3 # wrong way round
118+
@test isempty(interval(-1, -2) ^ -3) # wrong way round
119119
@test Interval(-3,2) ^ (3//1) == Interval(-27, 8)
120120
@test Interval(0.0) ^ 1.1 == Interval(0, 0)
121121
@test Interval(0.0) ^ 0.0 == emptyinterval()
@@ -432,4 +432,4 @@ end
432432
@test nthroot(Interval{BigFloat}(-27, 27), -3) == Interval{BigFloat}(-Inf, Inf)
433433
@test nthroot(Interval{BigFloat}(-81, -16), -4) ==
434434
@test nthroot(Interval{BigFloat}(-81, -16), 1) == Interval{BigFloat}(-81, -16)
435-
end
435+
end

0 commit comments

Comments
 (0)