Skip to content

Commit

Permalink
Add explicit error messages for boolean functions
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHnt committed Sep 19, 2023
1 parent 15cc40f commit bc963fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/intervals/interval_operations/boolean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ function isequalinterval(a::Interval, b::Interval)
isemptyinterval(a) && isemptyinterval(b) && return true
return inf(a) == inf(b) && sup(a) == sup(b)
end
Base.:(==)(::Interval, ::Interval) =
throw(ArgumentError("`==` is purposely not supported, use `isequalinterval` instead"))

"""
isequalinterval(a::Interval, x::Real)
Expand Down Expand Up @@ -165,7 +163,8 @@ function ismember(x::Real, a::Interval)
return inf(a) x sup(a)
end

ismember(::Interval, ::Interval) = throw(ArgumentError("ismember(::Interval, ::Interval) is not defined, maybe you meant isweaklysubset"))
ismember(::Interval, ::Interval) =
throw(ArgumentError("`ismember` is purposely not supported for two interval arguments. See instead `isweaklysubset`."))
ismember(x::Real, a::Complex{<:Interval}) = ismember(x, real(a)) && ismember(0, imag(a))
ismember(x::Complex, a::Complex{<:Interval}) = ismember(real(x), real(a)) && ismember(imag(x), imag(a))

Expand Down
29 changes: 29 additions & 0 deletions src/intervals/real_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,32 @@ hash(x::Interval, h::UInt) = hash(sup(x), hash(inf(x), hash(Interval, h)))

# TODO No idea where this comes from and if it is the correct place to put it.
dist(a::Interval, b::Interval) = max(abs(inf(a)-inf(b)), abs(sup(a)-sup(b)))

#

Base.:(==)(::Interval, ::Interval) = # also returned when calling `≤`, `≥`, `isequal`
throw(ArgumentError("`==` is purposely not supported for intervals. See instead `isequalinterval`."))

Base.:<(::Interval, ::Interval) = # also returned when calling `>`
throw(ArgumentError("`<` is purposely not supported for intervals. See instead `isweaklyless`, `precedes`."))

Base.isdisjoint(::Interval, ::Interval) =
throw(ArgumentError("`isdisjoint` is purposely not supported for intervals. See instead `isdisjointinterval`."))

Base.issubset(::Interval, ::Interval) =
throw(ArgumentError("`issubset` is purposely not supported for intervals. See instead `isweaklysubset`."))

Base.in(::Interval, ::Interval) =
throw(ArgumentError("`in` is purposely not supported for intervals. See instead `ismember`."))

Base.isempty(::Interval) =
throw(ArgumentError("`isempty` is purposely not supported for intervals. See instead `isemptyinterval`."))

Base.isfinite(::Interval) = # also returned when calling `isinf`
throw(ArgumentError("`isfinite` is purposely not supported for intervals. See instead `isbounded`."))

Base.isnan(::Interval) =
throw(ArgumentError("`isnan` is purposely not supported for intervals. See instead `isnai`."))

Base.isinteger(::Interval) =
throw(ArgumentError("`isinteger` is purposely not supported for intervals. See instead `isthininteger`."))
13 changes: 13 additions & 0 deletions test/interval_tests/consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,17 @@ import IntervalArithmetic: unsafe_interval
end
end

@testset "Disallowed `Real` functionalities" begin
x, y = interval(1), interval(2)
@test_throws ArgumentError x == y
@test_throws ArgumentError x < y
@test_throws ArgumentError isdisjoint(x, y)
@test_throws ArgumentError issubset(x, y)
@test_throws ArgumentError x in y
@test_throws ArgumentError isempty(x)
@test_throws ArgumentError isfinite(x)
@test_throws ArgumentError isnan(x)
@test_throws ArgumentError isinteger(x)
end

end

0 comments on commit bc963fb

Please sign in to comment.