-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StepRange{<:Integer} with non-integer step #32419
Comments
👍 Sounds good to me.
This would be breaking, since the start and step don't need to be promotable. E.g. |
Ah, I didn’t think about that. On second thought, I don’t like that idea of changing |
Technically, this would be a breaking change as well, since someone could have used these ranges. They work fine as long as their length is 0 or 1. One could think about only throwing an error if the length is >1. |
I think that's an appropriate "minor change" — yes, technically breaking so we should note it in NEWS.md, but unlikely to impact real code. And if it does appear in real code, it's likely a bug waiting to happen. |
I guess this and the related pull request can be closed. julia> StepRange(1,0.5,2)
ERROR: ArgumentError: StepRange should not be used with floating point
Stacktrace:
[1] steprange_last(::Int64, ::Float64, ::Int64) at .\range.jl:209
[2] StepRange{Int64,Float64}(::Int64, ::Float64, ::Int64) at .\range.jl:202
[3] StepRange(::Int64, ::Float64, ::Int64) at .\range.jl:253
[4] top-level scope at REPL[58]:1 though, now StepRangeLen has the problem: julia> StepRangeLen{Int}(1, 0.5, 2)
Error showing value of type StepRangeLen{Int64,Int64,Float64}:
ERROR: InexactError: Int64(0.5) but when omitting the types everything works: julia> StepRangeLen(1,0.5,2)
1.0:0.5:1.5 And from using |
Actually, the original case still errors: julia> r = StepRange(1, 1//3, 2)
1:1//3:2
julia> collect(r)
ERROR: InexactError: Int64(4//3)
Stacktrace:
[1] Integer at ./rational.jl:88 [inlined]
[2] convert at ./number.jl:7 [inlined]
[3] iterate at ./range.jl:598 [inlined]
[4] vcat(::StepRange{Int64,Rational{Int64}}) at ./range.jl:946
[5] collect(::StepRange{Int64,Rational{Int64}}) at ./range.jl:952
[6] top-level scope at REPL[2]:1 |
Since this affects |
It is possible to create a
StepRange{<:Integer}
with non-integer step:Should the creation of such ranges throw an error?
There are methods which expect a
StepRange{<:Integer}
to have an integer step (quite reasonable IMO), and which return wrong results if this is not the case:I would prefer the following behavior:
StepRange{T,S}(start, step, stop)
throws an error ifT<:Integer
and!isinteger(step)
StepRange(start, step, stop)
(constructor without type parameters) convertsstart
andstop
topromote_type(typeof(start), typeof(step), typeof(stop))
, so thatStepRange(1, 1//3, 2)
yields aStepRange{Rational{Int},Rational{Int}}
, butStepRange(1//2, 1, 5//2)
still yields aStepRange{Rational{Int},Int}
The text was updated successfully, but these errors were encountered: