Unexpected behavior when assigning value to variable compared to using the value itself

Hello,

I am still pretty new to Julia, so I’m sure this is a silly question, but I am having a hard time searching for an explanation. I am seeing some strange behavior I was hoping someone could clarify for me.

Essentially the following code gives me the expected output - it broadcasts the 1 to an array and then does element-wise division for every item in the divisors:

divisors = 1:2
1 ./ divisors

with this output

2-element Vector{Float64}:
1.0 
0.5

If I instead do not assign the range to a variable and just do:

1 ./ 1:2

I get the following output:

1.0:1.0:2.0

I’m not even seeing how this could be a possible output for this input, so I am very confused what might be happening here. Could someone explain what difference is happening under the hood to lead to the altered responses?
Thanks and sorry again if it’s a dumb question!!

1 Like

: has not-higher (specifically: lower) operator precedence than /, so it’s parsing 1 ./ 1:2 as (1 ./ 1):2, yielding 1.0:2. Try 1 ./ (1:2) instead.

4 Likes