Skip to content

Commit a712208

Browse files
lbenetdpsanders
authored andcommitted
Add methods to use .. notation with Irrationals (#144)
* Add methods to use .. notation with Irrationals * Two more tests
1 parent eecbd1f commit a712208

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/intervals/conversion.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ function atomic(::Type{Interval{T}}, x::S) where {T<:AbstractFloat, S<:AbstractF
116116
return atomic(Interval{T}, xrat)
117117
end
118118

119+
atomic(::Type{Interval{Irrational{T}}}, x::Irrational{S}) where {T, S} =
120+
float(atomic(Interval{Float64}, x))
121+
119122
function atomic(::Type{Interval{T}}, x::Interval) where T<:AbstractFloat
120123
Interval{T}( T(x.lo, RoundDown), T(x.hi, RoundUp) )
121124
end

src/intervals/intervals.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ function ..(a::T, b::S) where {T, S}
126126
interval(atomic(Interval{T}, a).lo, atomic(Interval{S}, b).hi)
127127
end
128128

129+
function ..(a::T, b::Irrational{S}) where {T, S}
130+
R = promote_type(T, Irrational{S})
131+
interval(atomic(Interval{R}, a).lo, atomic(Interval{R}, b).hi)
132+
end
133+
134+
function ..(a::Irrational{T}, b::S) where {T, S}
135+
R = promote_type(Irrational{T}, S)
136+
interval(atomic(Interval{R}, a).lo, atomic(Interval{R}, b).hi)
137+
end
138+
139+
function ..(a::Irrational{T}, b::Irrational{S}) where {T, S}
140+
R = promote_type(Irrational{T}, Irrational{S})
141+
interval(atomic(Interval{R}, a).lo, atomic(Interval{R}, b).hi)
142+
end
143+
129144
# ..(a::Integer, b::Integer) = interval(a, b)
130145
# ..(a::Integer, b::Real) = interval(a, nextfloat(float(b)))
131146
# ..(a::Real, b::Integer) = interval(prevfloat(float(a)), b)

test/interval_tests/construction.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ using Base.Test
3535
@test Interval{BigFloat}(1) == Interval{BigFloat}(big(1.0), big(1.0))
3636
@test Interval{BigFloat}(pi) == Interval{BigFloat}(big(pi), big(pi))
3737

38+
@test -pi..pi == @interval(-pi,pi)
39+
@test 0..pi == hull(interval(0), pi_interval(Float64))
40+
@test 1.2..pi == @interval(1.2, pi)
41+
@test pi..big(4) == hull(pi_interval(BigFloat), interval(4))
42+
@test pi..pi == pi_interval(Float64)
43+
@test eu..pi == hull(@interval(eu), pi_interval(Float64))
44+
3845
# a < Inf and b > -Inf
3946
@test @interval(1e300) == Interval(9.999999999999999e299, 1.0e300)
4047
@test @interval(-1e307) == Interval(-1.0000000000000001e307, -1.0e307)

0 commit comments

Comments
 (0)