Wrong type returned by range in 2-dims case?

When I tried the following command to generate 2-dims linspace arrays, whose type should be Float, not Int

r = range([1,1],[8,9],length=10)
# 10-element LinRange{Array{Int64,1}}:
# [1.0, 1.0],[1.77778, 1.88889],[2.55556, 2.77778],…,[7.22222, 8.11111],[8.0, 9.0]

and when I access r[2], it throws

ERROR: InexactError: Int64(1.7777777777777777)

But the LinRange works well,

lr = LinRange([1,1],[8,9],10)
# 10-element LinRange{Array{Float64,1}}:
# [1.0, 1.0],[1.77778, 1.88889],[2.55556, 2.77778],…,[7.22222, 8.11111],[8.0, 9.0]

So I am wondering if this is a possible bug returned by range in 2-dims case.

You told range to use Ints.

10-element LinRange{Array{Int64,1}}:

Do this instead.


julia> r = range([1.0,1.0],[8.0,9.0],length=10)
10-element LinRange{Array{Float64,1}}:
 [1.0, 1.0],[1.77778, 1.88889],[2.55556, 2.77778],…,[6.44444, 7.22222],[7.22222, 8.11111],[8.0, 9.0]

julia> r[3]
2-element Array{Float64,1}:
 2.5555555555555554
 2.7777777777777777

Thank you for your response. Actually, I have tried that way. But I am curious about the different behaviors of range and LinRange.

always good to start with the docs
https://docs.julialang.org/en/v1.2-dev/base/collections/#Base.LinRange
https://docs.julialang.org/en/v1.2-dev/base/math/#Base.range

1 Like

Thank you for your advice.

Actually, because I know that for 1-dim case, range(1, 8, length=10) would not be restricted to Int, and it is OK to access the second Float element. But for 2-dim case, it fails. So I am a little confused.

I am not sure range was intended to work with non-scalar arguments.