# How can I define a half-open interval in Julia, and test for containment?

**URL:** <https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787>\
**Category:** New to Julia\
**Created:** [December 8, 2021, 8:02pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787 "2021-12-08T20:02:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisoffner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisoffner/32/31162_2.png) [@chrisoffner](https://discourse.julialang.org/u/chrisoffner)\
**Post date:** [December 8, 2021, 8:02pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/1 "2021-12-08T20:02:26Z")

</div>

I’m new to Julia, and was trying to find a way to define a half-open interval like `(-Inf, 5.0]`, then test if a floating point number is contained therein.

In Swift I could do

```swift
let leftOpenInterval = ...5.0

print(leftOpenInterval.contains(-Double.infinity)) // true
print(leftOpenInterval.contains(3)) // true
print(leftOpenInterval.contains(6)) // false
print(leftOpenInterval.contains(Double.infinity)) // false

let rightOpenInterval = 5.0...

print(rightOpenInterval.contains(-Double.infinity)) // false
print(rightOpenInterval.contains(3)) // false
print(rightOpenInterval.contains(6)) // true
print(rightOpenInterval.contains(Double.infinity)) // true

```

How could I achieve the equivalent result in Julia? I tried doing `-Inf:5.0` and `5.0:Inf` but these give me errors. Any advice on how to proceed here?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 8, 2021, 8:04pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/2 "2021-12-08T20:04:42Z")

</div>

[https://invenia.github.io/Intervals.jl/stable/](https://invenia.github.io/Intervals.jl/stable/)

---

<div class="post-metadata">

**Author:** ![chrisoffner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisoffner/32/31162_2.png) [@chrisoffner](https://discourse.julialang.org/u/chrisoffner)\
**Post date:** [December 8, 2021, 8:13pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/3 "2021-12-08T20:13:56Z")

</div>

Thank you for the link. However, I can’t seem to find an example of how to define the kind of unbounded interval in my OP.

If I try to mirror the example `Interval{Closed,Closed}(1, 10)` and try out `Interval{Float64, Unbounded, Closed}(1, 5.0)` or `Interval{Float64, Unbounded, Closed}(5.0)` (or any variation without the `Float64` type annotation), I just get errors.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 8, 2021, 8:21pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/4 "2021-12-08T20:21:16Z")

</div>

Is `Interval{Float64,Closed,Closed}(-Inf, 5.)` what you want? (`-Inf..5.0` for short)

---

<div class="post-metadata">

**Author:** ![chrisoffner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisoffner/32/31162_2.png) [@chrisoffner](https://discourse.julialang.org/u/chrisoffner)\
**Post date:** [December 8, 2021, 8:22pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/5 "2021-12-08T20:22:53Z")

</div>

Indeed it is. Thank you very much! 🙂

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [December 8, 2021, 8:24pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/6 "2021-12-08T20:24:03Z")

</div>

[IntervalSets.jl](https://github.com/JuliaMath/IntervalSets.jl) also works:

```julia
julia> using IntervalSets

julia> 5.0..Inf
5.0..Inf

julia> 6.0 ∈ 5.0..Inf
true

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 8, 2021, 8:25pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/7 "2021-12-08T20:25:44Z")

</div>

What’s the difference? When should you use one vs the other?

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [December 8, 2021, 8:35pm UTC](https://discourse.julialang.org/t/how-can-i-define-a-half-open-interval-in-julia-and-test-for-containment/72787/8 "2021-12-08T20:35:44Z")

</div>

Not, sure but some discussion and links here: [Intervals.jl vs IntervalSets.jl?](https://discourse.julialang.org/t/intervals-jl-vs-intervalsets-jl/16173)
