Skip to content

Commit

Permalink
#1823 - Use Tolerance for getfield in comparison of sets of the same …
Browse files Browse the repository at this point in the history
…type (#1824)

* Update LazySet.jl

* define promotion rules in comparisons

* Add default tolerance for Int and for Rational

* Update src/Utils/comparisons.jl

Co-Authored-By: Christian Schilling <[email protected]>
  • Loading branch information
mforets and schillic authored Dec 12, 2019
1 parent 8cf9c30 commit 8f371b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Interfaces/LazySet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ function ≈(X::LazySet, Y::LazySet)
end

for f in fieldnames(typeof(X))
if !(getfield(X, f) getfield(Y, f))
if !_isapprox(getfield(X, f), getfield(Y, f))
return false
end
end
Expand Down
18 changes: 16 additions & 2 deletions src/Utils/comparisons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ set_atol(N::Type{NT}, ε::NT) where {NT<:Number} = begin
end

default_tolerance(N::Type{<:Number}) = error("default tolerance for numeric type $N is not defined")
default_tolerance(N::Type{<:Rational}) = Tolerance(zero(N), zero(N), zero(N))
default_tolerance(N::Type{<:Integer}) = Tolerance(zero(N), zero(N), zero(N))
default_tolerance(N::Type{<:AbstractFloat}) = Tolerance(Base.rtoldefault(N), sqrt(eps(N)), zero(N))

"""
Expand Down Expand Up @@ -147,8 +149,9 @@ arguments to a common numeric type, returning them as a tuple. The conversion
is such that the common type to which the values are converted can represent
them as faithfully as possible.
"""
_leq(x::N, y::M; kwargs...) where {N<:Real, M<:Real} =
_leq(promote(x, y)...; kwargs...)
function _leq(x::N, y::M; kwargs...) where {N<:Real, M<:Real}
return _leq(promote(x, y)...; kwargs...)
end

"""
_geq(x::Real, y::Real; [kwargs...])
Expand Down Expand Up @@ -242,6 +245,11 @@ function _isapprox(x::N, y::N;
end
end

# different numeric types with promotion
function _isapprox(x::N, y::M; kwargs...) where {N<:Real, M<:Real}
return _isapprox(promote(x, y)...; kwargs...)
end

# generic "dense"
function _isapprox(x::AbstractVector{N}, y::AbstractVector{N};
rtol::Real=_rtol(N),
Expand All @@ -268,6 +276,12 @@ function _isapprox(x::SparseVector{N}, y::SparseVector{N};
return x.nzind == y.nzind && _isapprox(x.nzval, y.nzval, rtol=rtol, ztol=ztol, atol=atol)
end

# different numeric types with promotion
function _isapprox(x::AbstractVector{N}, y::AbstractVector{M};
kwargs...) where {N<:Real, M<:Real}
_isapprox(promote(x, y)...; kwargs...)
end

"""
ispermutation(u::AbstractVector{T}, v::AbstractVector)::Bool where {T}
Expand Down

0 comments on commit 8f371b3

Please sign in to comment.