# Inconsistent behavior of Range

**URL:** <https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632>\
**Category:** General Usage\
**Created:** [December 13, 2018, 4:36pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632 "2018-12-13T16:36:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [December 13, 2018, 4:36pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632/1 "2018-12-13T16:36:33Z")

</div>

Following expression gives inconsistent results for different values of `n`.

```julia
r = 0.0:pi/n:pi

```

I expect it to be length `n+1` and include `pi`. For instance, for `n=24` and `n=26` the behavior is as I expected, but for `n=25` it does not include `pi`.

I guess rounding error in `pi/n` might be the reason. I am using `range(0.0,stop=pi,length=n+1)` to avoid this but I wanted to bring this up in case this is an unknown issue.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 13, 2018, 4:42pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632/2 "2018-12-13T16:42:32Z")

</div>

Unfortunately, this is a basic issue with floating-point arithmetic itself:

```nohighlight
julia> 25*(pi/25)
3.1415926535897936

julia> float(pi)
3.141592653589793

```

There’s no principle way to know that you intended the floating-point value `pi/25` to be 1/25 of `pi`. The correct way to express this range is with `range` as you are doing. This will soon be expressible as `range(0, pi, length=26)` which is slightly shorter and nicer.

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [December 13, 2018, 6:53pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632/3 "2018-12-13T18:53:40Z")

</div>

Thanks! Actually I noticed this while translating some of my code from Matlab. Apparently Matlab has a way of dealing with this: it works as expected for any `n` in Matlab. Do you plan to depreciate the colon syntax and/or `range(0.0,stop=pi,step=pi/n)`? If not, it might be nice to fix this.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 13, 2018, 7:03pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632/4 "2018-12-13T19:03:12Z")

</div>

Matlab just assumes that you meant to hit the endpoint exactly if you’re within a few (5 IIRC) ulps, which is… of highly questionable correctness. It’s possible this can be fixed in a more disciplined way, but it’s fairly tricky.

---

<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:** [December 13, 2018, 7:05pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632/5 "2018-12-13T19:05:08Z")

</div>

> [@aharoun](#):
>
> Apparently Matlab has a way of dealing with this: it works as expected for any `n` in Matlab.

See also the discussion in [ranges with ambiguous lengths · Issue #20532 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/20532) and [Issues · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/2333%E2%80%A6) Matlab handles this by rounding the endpoint to a few ulps.

For example, `0:(pi/25)*(1 + 3e-16):pi` in Matlab also has length 26. The tradeoff with this is that the n-th point is no longer equal to `initial + (n-1)*step`. If you round too much, you can also increase the variation in the step size (i.e. `diff(diff(somerange))`), but Matlab’s endpoint rounding is small enough that this variation doesn’t seem worse than what we get anyway from roundoff errors in the cases I’ve checked.

---

<div class="post-metadata">

**Author:** ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)\
**Post date:** [December 13, 2018, 7:17pm UTC](https://discourse.julialang.org/t/inconsistent-behavior-of-range/18632/6 "2018-12-13T19:17:13Z")

</div>

`range(0, stop=pi, length=n+1)` is the “Julian way” to go then. Thanks guys.
