Skip to content

Commit a905a41

Browse files
authored
Promote bounds of Interval if they have different types (#2577)
1 parent 278fba5 commit a905a41

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/sets.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ struct Interval{T<:Real} <: AbstractScalarSet
286286
upper::T
287287
end
288288

289+
function Interval(lower::Real, upper::Real)
290+
return Interval(promote(lower, upper)...)
291+
end
292+
289293
function Base.:(==)(a::Interval{T}, b::Interval{T}) where {T}
290294
return a.lower == b.lower && a.upper == b.upper
291295
end
@@ -390,6 +394,10 @@ struct Semicontinuous{T<:Real} <: AbstractScalarSet
390394
upper::T
391395
end
392396

397+
function Semicontinuous(lower::Real, upper::Real)
398+
return Semicontinuous(promote(lower, upper)...)
399+
end
400+
393401
function Base.:(==)(a::Semicontinuous{T}, b::Semicontinuous{T}) where {T}
394402
return a.lower == b.lower && a.upper == b.upper
395403
end
@@ -422,6 +430,10 @@ struct Semiinteger{T<:Real} <: AbstractScalarSet
422430
upper::T
423431
end
424432

433+
function Semiinteger(lower::Real, upper::Real)
434+
return Semiinteger(promote(lower, upper)...)
435+
end
436+
425437
function Base.:(==)(a::Semiinteger{T}, b::Semiinteger{T}) where {T}
426438
return a.lower == b.lower && a.upper == b.upper
427439
end

test/sets.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,20 @@ function runtests()
363363
end
364364
end
365365

366+
function test_interval_promote()
367+
for S in (MOI.Interval, MOI.Semiinteger, MOI.Semicontinuous)
368+
set = S(1.0, π)
369+
@test set isa S{Float64}
370+
@test set.lower == 1.0
371+
@test set.upper π
372+
set = S(big(1), 2)
373+
@test set isa S{BigInt}
374+
@test set.lower == 1
375+
@test set.upper == 2
376+
end
377+
return
378+
end
379+
366380
end
367381

368382
TestSets.runtests()

0 commit comments

Comments
 (0)