Why does the range
function not like to construct a range with two equal Int32
s 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!
That has to be a bug. I'd create an issue.
@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.
it's kind of a bug, but you're basically asking for a non existing range?
True, but the behavior should be consistent for both integer types
The output of that is just 10 values of 1.0, could you just use ones(Int32, 10)
? (Or whatever output type you need)
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
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
Agreed
Mixing types also works for unequal values. range(Int32(1), Int64(10); length=10)
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
Yeah, I'd think it would be the other way around :joy:
Thanks everyone for your help, I've opened an issue here.
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)
. :)
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.
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