# Why are empty ranges normalized?

**URL:** <https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836>\
**Category:** General Usage\
**Tags:** range\
**Created:** [February 6, 2024, 8:35pm UTC](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836 "2024-02-06T20:35:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [February 6, 2024, 8:35pm UTC](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836/1 "2024-02-06T20:35:18Z")

</div>

What the motivation for this normalization?

```julia
julia> 5:1
5:4

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 6, 2024, 10:29pm UTC](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836/2 "2024-02-06T22:29:06Z")

</div>

There is a [comment in the source code](https://github.com/JuliaLang/julia/blob/101aa3911d438580921a6e9721f193d9694c26d8/base/range.jl#L366-L367) that this “simplifies arithmetic for Signed numbers”. This dates back to [Make StepRange safe for more number types by timholy · Pull Request #17611 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/17611)

For example, [`length(r)` can be computed by `last(r) - first(r) + 1`](https://github.com/JuliaLang/julia/blob/101aa3911d438580921a6e9721f193d9694c26d8/base/range.jl#L827) for unit-step ranges.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 7, 2024, 1:24am UTC](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836/3 "2024-02-07T01:24:29Z")

</div>

Oh, I have stepped in this landmine before. I had some code that passed two numbers around that were often used to define a range, so I started passing them around as a range. When the range was empty I still needed/wanted to know which were the values for other computations. Almost got crazy debugging not understanding where my data was getting corrupted.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [February 7, 2024, 2:44am UTC](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836/4 "2024-02-07T02:44:41Z")

</div>

> <https://github.com/JuliaLang/julia/issues/52195>
>
> See https://github.com/JuliaLang/julia/issues/51793. The issue is to change this… in Julia 2.0. Could introduce \`start(r)\` and \`stop(r)\` only for ranges to do what \`first(r)\` and \`last(r)\` currently do.

> Since we only normalize in certain cases, and thus cannot rely on it generally, we could consider removing the normalization, if you want to make a PR to try

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [February 7, 2024, 7:43am UTC](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836/5 "2024-02-07T07:43:26Z")

</div>

> [@stevengj](#):
>
> For example, [`length(r)` can be computed by `last(r) - first(r) + 1`](https://github.com/JuliaLang/julia/blob/101aa3911d438580921a6e9721f193d9694c26d8/base/range.jl#L827) for unit-step ranges.

This is not quite true - e.g. `typemin(Int):typemax(Int)` will return the wrong result.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 7, 2024, 8:55am UTC](https://discourse.julialang.org/t/why-are-empty-ranges-normalized/109836/6 "2024-02-07T08:55:16Z")

</div>

This doesn’t look good:

```julia
julia> length(typemin(Int)+1:0)
-9223372036854775808

julia> length(0:typemax(Int))
-9223372036854775808

```

but then what can we do while staying type stable… I guess throwing an error would be better.
