Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure boolean functions error for non-thin intervals #613

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions src/intervals/real_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,18 @@ end
==(::Number, ::Interval)
Test whether an interval is the singleton of a given number. In other words, the
result is true if and only if the interval contains only that number.
result is true if and only if the interval contains only that number. This
function errors whenever the input interval is not a singleton.
!!! note
Comparison between intervals is purposely disallowed. Indeed, equality
between non-singleton intervals has distinct properties, notably ``x = y``
does not imply ``x - y = 0``. See instead [`isequal_interval`](@ref).
"""
Base.:(==)(x::BareInterval, y::Number) = inf(x) == sup(x) == y
function Base.:(==)(x::BareInterval, y::Number)
isthin(x) || return throw(ArgumentError("`==` is only supported between thin intervals and numbers"))
return inf(x) == y
end
Base.:(==)(x::Number, y::BareInterval) = y == x
function Base.:(==)(x::Interval, y::Number)
isnai(x) && return false
Expand All @@ -154,19 +158,49 @@ Base.:(==)(x::Number, y::Interval) = y == x
Base.:(==)(x::BareInterval, y::Interval) = throw(MethodError(==, (x, y)))
Base.:(==)(x::Interval, y::BareInterval) = throw(MethodError(==, (x, y)))

Base.iszero(x::BareInterval) = iszero(inf(x)) & iszero(sup(x))
"""
iszero(::BareInterval)
iszero(::Interval)
Test whether an interval is the singleton of zero. This function errors whenever
the input interval is not a singleton.
"""
function Base.iszero(x::BareInterval)
isthin(x) || return throw(ArgumentError("`iszero` is only supported for thin intervals"))
return iszero(inf(x))
end
function Base.iszero(x::Interval)
isnai(x) && return false
return iszero(bareinterval(x))
end

Base.isone(x::BareInterval) = isone(inf(x)) & isone(sup(x))
"""
isone(::BareInterval)
isone(::Interval)
Test whether an interval is the singleton of one. This function errors whenever
the input interval is not a singleton.
"""
function Base.isone(x::BareInterval)
isthin(x) || return throw(ArgumentError("`isone` is only supported for thin intervals"))
return isone(inf(x))
end
function Base.isone(x::Interval)
isnai(x) && return false
return isone(bareinterval(x))
end

Base.isinteger(x::BareInterval) = (inf(x) == sup(x)) & isinteger(inf(x))
"""
isinteger(::BareInterval)
isinteger(::Interval)
Test whether an interval is the singleton of an integer. This function errors
whenever the input interval is not a singleton.
"""
function Base.isinteger(x::BareInterval)
isthin(x) || return throw(ArgumentError("`isinteger` is only supported for thin intervals"))
return isinteger(inf(x))
end
function Base.isinteger(x::Interval)
isnai(x) && return false
return isinteger(bareinterval(x))
Expand Down
Loading