Skip to content

Commit 7736bf6

Browse files
Add methods for float(::Interval) (#96)
* add methods for float(::Interval) * add tests for float(::Interval) * remove unnecessary float Co-authored-by: Sheehan Olver <[email protected]> Co-authored-by: Sheehan Olver <[email protected]>
1 parent 37759b8 commit 7736bf6

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/IntervalSets.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module IntervalSets
22

33
using Base: @pure
44
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash,
5-
union, intersect, minimum, maximum, extrema, range, clamp, , ,
5+
union, intersect, minimum, maximum, extrema, range, clamp, float, , ,
66

77
using Statistics
88
import Statistics: mean

src/interval.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,6 @@ end
232232
ClosedInterval{T}(i::AbstractUnitRange{I}) where {T,I<:Integer} = ClosedInterval{T}(minimum(i), maximum(i))
233233
ClosedInterval(i::AbstractUnitRange{I}) where {I<:Integer} = ClosedInterval{I}(minimum(i), maximum(i))
234234

235-
236-
237235
Base.promote_rule(::Type{Interval{L,R,T1}}, ::Type{Interval{L,R,T2}}) where {L,R,T1,T2} = Interval{L,R,promote_type(T1, T2)}
236+
237+
float(i::Interval{L, R, T}) where {L,R,T} = Interval{L, R, float(T)}(endpoints(i)...)

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,5 +729,24 @@ struct IncompleteInterval <: AbstractInterval{Int} end
729729
@test_throws ErrorException closedendpoints(I)
730730
end
731731

732+
@testset "float" begin
733+
i1 = 1..2
734+
@test i1 isa ClosedInterval{Int}
735+
@test float(i1) isa ClosedInterval{Float64}
736+
@test float(i1) == i1
737+
i2 = big(1)..2
738+
@test i2 isa ClosedInterval{BigInt}
739+
@test float(i2) isa ClosedInterval{BigFloat}
740+
@test float(i2) == i2
741+
i3 = OpenInterval(1,2)
742+
@test i3 isa OpenInterval{Int}
743+
@test float(i3) isa OpenInterval{Float64}
744+
@test float(i3) == i3
745+
i4 = OpenInterval(1.,2.)
746+
@test i4 isa OpenInterval{Float64}
747+
@test float(i4) isa OpenInterval{Float64}
748+
@test float(i4) == i4
749+
end
750+
732751
include("findall.jl")
733752
end

0 commit comments

Comments
 (0)