# Is this a bug (scalar+ranges) with the parser?

**URL:** <https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670>\
**Category:** Internals & Design\
**Tags:** broadcasting, range\
**Created:** [October 30, 2021, 7:45pm UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670 "2021-10-30T19:45:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [October 30, 2021, 7:45pm UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/1 "2021-10-30T19:45:44Z")

</div>

I want to add one scalar value with all value of a unitRange.

Naively, I would do the `x = 1:5 .+ 0.5` code, but I realize that actually the parser is doing `y=1:(5 .+ 0.5)`. Below a full example:

```julia
expected = [1,2,3,4,5] .+ 0.5

x = 1:5 .+ 0.5
y = 1:(5 .+ 0.5)
z = collect(1:5) .+ 0.5

@assert expected==x # not OK
@assert expected==y # not OK
@assert expected==z # OK

```

I achieve my intended solution with a `collect`. Case closed.  
However, I expected that the `y = 1:(5 .+ 0.5)` case, to be valid **only if** the user don’t write the dot operator - `y = 1:(5 + 0.5)`.

I am here to ask: is the parser doing, what it should be doing ?  
🤔

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 30, 2021, 7:50pm UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/2 "2021-10-30T19:50:39Z")

</div>

> [@Noel\_Araujo](#):
>
> `x = 1:5 .+ 0.5`

```julia
x = (1:5) .+ 0.5

```

Don’t use `collect`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 30, 2021, 7:54pm UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/3 "2021-10-30T19:54:31Z")

</div>

> [@Noel\_Araujo](#):
>
> the parser doing, what it should be doing ?

Yes, `+` has higher precedence than `:`, so this is expected and correct.

Numbers are iterables (this is somewhat controversial, though), so it is also expected that `5 .+ 0.5` is 5.5.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 30, 2021, 8:35pm UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/4 "2021-10-30T20:35:23Z")

</div>

`1:5 .+ 0.5` is such a dangerous pattern, I’ve been bitten more than once (in more languages than Julia).

I’m going to add a warning coloring for this in my editor, there is no reason to not specify `(1:5) .+ 0.5` or `1:(5 .+ 0.5)` (and so on for `*/-`)

edit: This is in my vim ftplugin for julia now. It will catch the simplest cases of this mistake.

```nohighlight
syn match juliaRangeArithemticWarning "(\@<![^(,;]\+:[^(,;]\+\s*\.\?[+\-/*^]"
hi def link juliaRangeArithemticWarning ErrorMsg

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 30, 2021, 11:48pm UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/5 "2021-10-30T23:48:17Z")

</div>

> [@gustaphe](#):
>
> `1:5 .+ 0.5` is such a dangerous pattern

Linking [this related post](https://discourse.julialang.org/t/unitrange-in-1-7-rc1-bug/68327), where one danger is illustrated by:

```julia
v = (1:5) .- 1/7
length(v) == 4 # true  

v = collect(1:5) .- 1/7
length(v) == 5 # true  

```

_ **NB:** using Julia 1.7_

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 30, 2021, 11:56pm UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/6 "2021-10-30T23:56:50Z")

</div>

Thanks I hate it

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [October 31, 2021, 2:54am UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/7 "2021-10-31T02:54:21Z")

</div>

That’s just a bug. I think it may already be fixed.

---

<div class="post-metadata">

**Author:** ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)\
**Post date:** [October 31, 2021, 8:02am UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/8 "2021-10-31T08:02:35Z")

</div>

Fixed in 1.7.0-rc2

[https://github.com/JuliaLang/julia/issues/42291](https://github.com/JuliaLang/julia/issues/42291)

```julia
julia> v = (1:5) .- 1/7
0.8571428571428572:1.0:4.857142857142858

julia> length(v)
5

julia> versioninfo()
Julia Version 1.7.0-rc2
Commit f23fc0d27a* (2021-10-20 12:45 UTC)
DEBUG build
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-9400F CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

```

But …

```julia
julia> ( (1:5) .- 1/7 ) == ( collect(1:5) .- 1/7 )
false

julia> collect( (1:5) .- 1/7 ) == ( collect(1:5) .- 1/7 )
false

julia> collect( (1:5) .- 1/7 )
5-element Vector{Float64}:
 0.8571428571428572
 1.8571428571428572
 2.857142857142857
 3.857142857142857
 4.857142857142858

julia> ( collect(1:5) .- 1/7 )
5-element Vector{Float64}:
 0.8571428571428572
 1.8571428571428572
 2.857142857142857
 3.857142857142857
 4.857142857142857

```

Interesting floating point numbers

```julia
julia> 1 - 1/7 + 4
4.857142857142858

julia> 1 + 4 - 1/7
4.857142857142857

julia> (1 - 1/7 + 4) == (1 + 4 - 1/7)
false

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 31, 2021, 9:04am UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/9 "2021-10-31T09:04:12Z")

</div>

> [@woclass](#):
>
> Fixed in 1.7.0-rc2

Do not have this latest release installed but could you confirm if the following is true?

```julia
v = (1:5) .- 1/7
length(v) == 5 # true

v = (1 - 1/7):(5 - 1/7)
length(v) == 5 # false

```

---

<div class="post-metadata">

**Author:** ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)\
**Post date:** [October 31, 2021, 9:06am UTC](https://discourse.julialang.org/t/is-this-a-bug-scalar-ranges-with-the-parser/70670/10 "2021-10-31T09:06:41Z")

</div>

```julia
julia> v = (1:5) .- 1/7
0.8571428571428572:1.0:4.857142857142858

julia> length(v) == 5
true

julia>

julia> v = (1 - 1/7):(5 - 1/7)
0.8571428571428572:1.0:3.857142857142857

julia> length(v) == 5
false

julia> versioninfo()
Julia Version 1.7.0-rc2
Commit f23fc0d27a* (2021-10-20 12:45 UTC)
DEBUG build
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-9400F CPU @ 2.90GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

```
