Stream: helpdesk (published)

Topic: range() with two equal Int32s


view this post on Zulip Hannes (Feb 23 2021 at 10:02):

Why does the range function not like to construct a range with two equal Int32s as inputs? It seems to work when one of the inputs is an Int32 but not both.

julia> range(Int32(1),Int64(1);length=10)
1.0:0.0:1.0

julia> range(Int64(1),Int32(1);length=10)
1.0:0.0:1.0

julia> range(Int32(1),Int32(1);length=10)
ERROR: MethodError: Cannot `convert` an object of type Tuple{Int32,Int64} to an object of type Float64

I think there's just something I don't understand about the range function :/ Thanks!

view this post on Zulip Zachary P Christensen (Feb 23 2021 at 11:37):

That has to be a bug. I'd create an issue.

view this post on Zulip Hannes (Feb 23 2021 at 11:42):

@Zachary P Christensen Thanks, I was wondering if it was a bug. If I don't hear any more on this thread I'll open an issue.

view this post on Zulip Mosè Giordano (Feb 23 2021 at 12:19):

it's kind of a bug, but you're basically asking for a non existing range?

view this post on Zulip Zachary P Christensen (Feb 23 2021 at 12:21):

True, but the behavior should be consistent for both integer types

view this post on Zulip Michael Alexander (Feb 23 2021 at 12:21):

The output of that is just 10 values of 1.0, could you just use ones(Int32, 10) ? (Or whatever output type you need)

view this post on Zulip Mosè Giordano (Feb 23 2021 at 12:21):

julia> range(Int32(1), Int32(10); length=10)
1.0:1.0:10.0

julia> range(Int32(1), Int32(9); length=10)
1.0:0.8888888888888888:9.0

work as expected

view this post on Zulip Mosè Giordano (Feb 23 2021 at 12:22):

Zachary P Christensen said:

True, but the behavior should be consistent for both integer types

it's certainly inconsistent, but I find more surprising that range(1, 1; length=10) doesn't error out

view this post on Zulip Zachary P Christensen (Feb 23 2021 at 12:23):

Agreed

view this post on Zulip Michael Alexander (Feb 23 2021 at 12:24):

Mixing types also works for unequal values. range(Int32(1), Int64(10); length=10)

view this post on Zulip Mosè Giordano (Feb 23 2021 at 12:26):

I find this more incosistent:

julia> 1.0:0.0:1.0
ERROR: ArgumentError: range step cannot be zero
Stacktrace:
 [1] (::Colon)(start::Float64, step::Float64, stop::Float64)
   @ Base ./twiceprecision.jl:387
 [2] top-level scope
   @ REPL[24]:1

julia> range(1, 1; length=10)
1.0:0.0:1.0

view this post on Zulip Simeon Schaub (Feb 23 2021 at 15:28):

Yeah, I'd think it would be the other way around :joy:

view this post on Zulip Hannes (Feb 23 2021 at 18:56):

Thanks everyone for your help, I've opened an issue here.

view this post on Zulip Hannes (Feb 23 2021 at 18:58):

Michael Alexander said:

The output of that is just 10 values of 1.0, could you just use ones(Int32, 10) ? (Or whatever output type you need)

Normally I would, in this case I want to get n evenly spaced values from the minimum to the maximum of an array. I'll just add an explicit check for when minimum(x) == maximum(x). :)

view this post on Zulip Mark Kittisopikul (Feb 24 2021 at 09:16):

I have a fix for this in https://github.com/JuliaLang/julia/pull/39808 . one might be better than oneunit here though. I need to think about this with more sleep.

view this post on Zulip Mark Kittisopikul (Feb 24 2021 at 09:41):

You might be interested in just using LinRange.

julia> LinRange(1, 2, 10)
10-element LinRange{Float64}:
 1.0,1.11111,1.22222,1.33333,1.44444,1.55556,1.66667,1.77778,1.88889,2.0

julia> LinRange(1, 2, 9)
9-element LinRange{Float64}:
 1.0,1.125,1.25,1.375,1.5,1.625,1.75,1.875,2.0

julia> LinRange(1, 2, 11)
11-element LinRange{Float64}:
 1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0

julia> LinRange(Int32(1), Int32(1), 10)
10-element LinRange{Float64}:
 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0

Last updated: Nov 22 2024 at 04:41 UTC