Skip to content

Commit

Permalink
Promote bounds of Interval if they have different types (#2577)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Nov 3, 2024
1 parent 278fba5 commit a905a41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ struct Interval{T<:Real} <: AbstractScalarSet
upper::T
end

function Interval(lower::Real, upper::Real)
return Interval(promote(lower, upper)...)
end

function Base.:(==)(a::Interval{T}, b::Interval{T}) where {T}
return a.lower == b.lower && a.upper == b.upper
end
Expand Down Expand Up @@ -390,6 +394,10 @@ struct Semicontinuous{T<:Real} <: AbstractScalarSet
upper::T
end

function Semicontinuous(lower::Real, upper::Real)
return Semicontinuous(promote(lower, upper)...)
end

function Base.:(==)(a::Semicontinuous{T}, b::Semicontinuous{T}) where {T}
return a.lower == b.lower && a.upper == b.upper
end
Expand Down Expand Up @@ -422,6 +430,10 @@ struct Semiinteger{T<:Real} <: AbstractScalarSet
upper::T
end

function Semiinteger(lower::Real, upper::Real)
return Semiinteger(promote(lower, upper)...)
end

function Base.:(==)(a::Semiinteger{T}, b::Semiinteger{T}) where {T}
return a.lower == b.lower && a.upper == b.upper
end
Expand Down
14 changes: 14 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ function runtests()
end
end

function test_interval_promote()
for S in (MOI.Interval, MOI.Semiinteger, MOI.Semicontinuous)
set = S(1.0, π)
@test set isa S{Float64}
@test set.lower == 1.0
@test set.upper π
set = S(big(1), 2)
@test set isa S{BigInt}
@test set.lower == 1
@test set.upper == 2
end
return
end

end

TestSets.runtests()

0 comments on commit a905a41

Please sign in to comment.