# Stop of a range with length 0

**URL:** https://discourse.julialang.org/t/stop-of-a-range-with-length-0/99865
**Category:** Internals & Design
**Tags:** question, range
**Created:** [June 5, 2023, 2:25am UTC](https://discourse.julialang.org/t/stop-of-a-range-with-length-0/99865 "2023-06-05T02:25:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abs/32/50494_2.png) [@abs](https://discourse.julialang.org/u/abs)
#### Post date: [June 5, 2023, 2:25am UTC](https://discourse.julialang.org/t/stop-of-a-range-with-length-0/99865/1 "2023-06-05T02:25:17Z")

</div>

Just curious:

For the following kind of ranges of length 0, please note the submitted stop and returned

```julia
julia> 12:1: **3**
12:1: **11**

julia> range(12, **stop=3** ;step = 1)
12:1: **11**

```

Is this by design?

`_range(start::Any , step::Any , stop::Any , len::Nothing) = range_start_step_stop(start, step, stop)`

> <https://github.com/JuliaLang/julia/blob/8e630552924eac54c809aa7bc30871c7df1582d3/base/range.jl#L164>

`range_start_step_stop(start, step, stop) = start:step:stop`

> <https://github.com/JuliaLang/julia/blob/8e630552924eac54c809aa7bc30871c7df1582d3/base/range.jl#L216>

Thanks.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 5, 2023, 3:30am UTC](https://discourse.julialang.org/t/stop-of-a-range-with-length-0/99865/2 "2023-06-05T03:30:47Z")

</div>

Yes, the `StepRange` constructor calls `steprange_last(start, step, stop)` to recompute the `stop` value. The purpose is to have 1 unique instance represent each non-empty sequence; this makes comparisons `1:2:4 === 1:2:3` easier, which helps things like hashing. An empty sequence does not have 1 unique instance, but that call does limit the variability of `stop` relative to `start`.

---

<div class="post-metadata">

### Author: ![abs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abs/32/50494_2.png) [@abs](https://discourse.julialang.org/u/abs)
#### Post date: [June 5, 2023, 4:07am UTC](https://discourse.julialang.org/t/stop-of-a-range-with-length-0/99865/3 "2023-06-05T04:07:17Z")

</div>

So all ranges of length zero, regardless of their start, stop and step values, are considered equal - the reasons make sense, thanks.

Good to be aware not to expect range stop getting round-tripped under all conditions.

Thanks!

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [June 5, 2023, 6:49am UTC](https://discourse.julialang.org/t/stop-of-a-range-with-length-0/99865/4 "2023-06-05T06:49:29Z")

</div>

You get equal for all zero length ranges since they represent the same value (a range with no items).  
You get identical only for zero length ranges with the same start and step, since they are stored exactly the same.

```julia
julia> 5:2:4 == 5:2:3
true

julia> 4:2:3 == 5:2:3
true

julia> 5:1:4 == 5:2:4
true

julia> 5:2:4 === 5:2:3
true

julia> 4:2:3 === 5:2:3
false

julia> 5:1:4 === 5:2:4
false

```
