 Range vs. Array

I would like to understand why the two statements below generate different results. I was expecting both results to be the same (equal to the one produced by the second statement). I’m running Julia Version 1.1.0 (2019-01-21).

``````julia> sqrt.(10.0 * 1:100)
91-element Array{Float64,1}:
3.1622776601683795
3.3166247903554
3.4641016151377544
3.605551275463989
3.7416573867739413
3.872983346207417
⋮
9.746794344808963
9.797958971132712
9.848857801796104
9.899494936611665
9.9498743710662
10.0

julia> sqrt.(10.0 * collect(1:100))
100-element Array{Float64,1}:
3.1622776601683795
4.47213595499958
5.477225575051661
6.324555320336759
7.0710678118654755
7.745966692414834
⋮
30.822070014844883
30.983866769659336
31.144823004794873
31.304951684997057
31.464265445104548
31.622776601683793
``````

`*` has a higher precedence than `:`, so your first example is equivalent to `sqrt.((10.0 * 1):100)`. You’re probably looking for `sqrt.(10.0 .* (1:100))`.

4 Likes

Ah, beat me by like 10 seconds 1 Like