Skip to content

Commit 0661291

Browse files
authored
define isapprox (#125)
* define isapprox * after review
1 parent fade689 commit 0661291

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/IntervalSets.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module IntervalSets
22

33
using Base: @pure
4-
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash,
4+
import Base: eltype, convert, show, in, length, isempty, isequal, isapprox, issubset, ==, hash,
55
union, intersect, minimum, maximum, extrema, range, clamp, mod, float, , ,
66

77
using Statistics
@@ -142,6 +142,16 @@ isequal(A::TypedEndpointsInterval, B::TypedEndpointsInterval) = isempty(A) & ise
142142
==(A::TypedEndpointsInterval{L,R}, B::TypedEndpointsInterval{L,R}) where {L,R} = (leftendpoint(A) == leftendpoint(B) && rightendpoint(A) == rightendpoint(B)) || (isempty(A) && isempty(B))
143143
==(A::TypedEndpointsInterval, B::TypedEndpointsInterval) = isempty(A) && isempty(B)
144144

145+
function isapprox(A::AbstractInterval, B::AbstractInterval; atol=0, rtol=Base.rtoldefault(eltype(A), eltype(B), atol), kwargs...)
146+
closedendpoints(A) != closedendpoints(B) && error("Comparing intervals with different closedness is not defined")
147+
isempty(A) != isempty(B) && return false
148+
isempty(A) && isempty(B) && return true
149+
maxabs = max(maximum(abs, endpoints(A)), maximum(abs, endpoints(B)))
150+
let atol = max(atol, rtol * maxabs)
151+
isapprox(leftendpoint(A), leftendpoint(B); atol, rtol, kwargs...) && isapprox(rightendpoint(A), rightendpoint(B); atol, rtol, kwargs...)
152+
end
153+
end
154+
145155
function issubset(A::TypedEndpointsInterval, B::TypedEndpointsInterval)
146156
Al, Ar = endpoints(A)
147157
Bl, Br = endpoints(B)

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ struct IncompleteInterval <: AbstractInterval{Int} end
150150
@test isempty(ClosedInterval(B, A))
151151
end
152152

153+
@testset "isapprox" begin
154+
@test 1..2 1..2
155+
@test 1..2 (1+1e-10)..2
156+
@test 1..2 1..2.01
157+
@test 10..11 10.1..10.9 rtol=0.01
158+
@test 10..11 10.1..10.9 atol=0.1
159+
@test 10..11 10.1..10.9 rtol=0.005
160+
@test 10..11 10.1..10.9 atol=0.05
161+
@test 0..1 eps()..1
162+
@test 100.0..100.0 nextfloat(100.0)..100.0
163+
@test 3..1 5..1
164+
@test_throws Exception OpenInterval(0, 1) ClosedInterval(0, 1)
165+
end
166+
153167
@testset "Convert" begin
154168
I = 0..3
155169
@test @inferred(convert(ClosedInterval{Float64}, I)) ===

0 commit comments

Comments
 (0)