From a905a415b2d7d156e8a299ff12df3b325533fe4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 3 Nov 2024 19:54:48 +0100 Subject: [PATCH] Promote bounds of Interval if they have different types (#2577) --- src/sets.jl | 12 ++++++++++++ test/sets.jl | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/sets.jl b/src/sets.jl index 70ab9430c6..3cc6f42572 100644 --- a/src/sets.jl +++ b/src/sets.jl @@ -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 @@ -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 @@ -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 diff --git a/test/sets.jl b/test/sets.jl index 28b0c5000a..bf52326974 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -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()