Skip to content

Commit 1e1dad2

Browse files
authored
Fix cancelminus (#509)
* Fix cancelminus * Bump patch version * Add few tests related with one corner case mentioned in the standard
1 parent 07d74b7 commit 1e1dad2

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "IntervalArithmetic"
22
uuid = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
33
repo = "https://github.com/JuliaIntervals/IntervalArithmetic.jl.git"
4-
version = "0.20.3"
4+
version = "0.20.4"
55

66
[deps]
77
CRlibm = "96374032-68de-5a5b-8d9e-752f78720389"

src/intervals/arithmetic.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -531,21 +531,21 @@ function cancelminus(a::Interval{T}, b::Interval{T}) where T<:Real
531531
c_lo = @round_down(a.lo - b.lo)
532532
c_hi = @round_up(a.hi - b.hi)
533533

534+
# Interval(c_lo, c_hi) is not a proper interval
534535
c_lo > c_hi && return entireinterval(T)
536+
c = Interval(c_lo, c_hi)
535537

536-
c_lo == Inf && return Interval(prevfloat(c_lo), c_hi)
537-
c_hi == -Inf && return Interval(c_lo, nextfloat(c_hi))
538+
# Corner case 2 (page 62), involving unbounded c
539+
(c_lo == Inf || c_hi == -Inf) && return widen(c)
540+
isunbounded(c) && return c
538541

542+
# Corner case 1 (page 62) involving finite precision for diam(a) and diam(b)
539543
a_lo = @round_down(b.lo + c_lo)
540544
a_hi = @round_up(b.hi + c_hi)
545+
(diam(a) == diam(b)) && (nextfloat(a.hi) < a_hi || prevfloat(a.lo) > a_lo) &&
546+
return entireinterval(T)
541547

542-
if a_lo a.lo a.hi a_hi
543-
(nextfloat(a.hi) < a_hi || prevfloat(a.lo) > a_hi) &&
544-
return entireinterval(T)
545-
return Interval(c_lo, c_hi)
546-
end
547-
548-
return entireinterval(T)
548+
return c
549549
end
550550
cancelminus(a::Interval, b::Interval) = cancelminus(promote(a, b)...)
551551

test/interval_tests/consistency.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ setprecision(Interval, Float64)
262262
@test cancelplus(Interval(0.0), Interval(1.0)) == Interval(1.0)
263263
@test cancelminus(Interval(-5.0, 0.0), Interval(0.0, 5.0)) == Interval(-5.0)
264264
@test cancelplus(Interval(-5.0, 0.0), Interval(0.0, 5.0)) == Interval(0.0)
265+
@test cancelminus(Interval(1e308), -Interval(1e308)) == widen(Interval(Inf))
266+
@test cancelplus(Interval(1e308), Interval(1e308)) == widen(Interval(Inf))
267+
@test cancelminus(Interval(nextfloat(1e308)), -Interval(nextfloat(1e308))) ==
268+
widen(Interval(Inf))
269+
@test cancelplus(Interval(nextfloat(1e308)), Interval(nextfloat(1e308))) ==
270+
widen(Interval(Inf))
271+
@test cancelminus(Interval(prevfloat(big(Inf))), -Interval(prevfloat(big(Inf)))) ==
272+
widen(Interval(big(Inf)))
273+
@test cancelplus(Interval(prevfloat(big(Inf))), Interval(prevfloat(big(Inf)))) ==
274+
widen(Interval(big(Inf)))
265275
end
266276

267277
@testset "mid and radius" begin

0 commit comments

Comments
 (0)