Skip to content
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

getindex for infinite range #17594

Closed
pwl opened this issue Jul 24, 2016 · 8 comments
Closed

getindex for infinite range #17594

pwl opened this issue Jul 24, 2016 · 8 comments
Labels
needs tests Unit tests are required for this change

Comments

@pwl
Copy link
Contributor

pwl commented Jul 24, 2016

It seems to me that this should work

r=1:Inf
r[2]   # should give 2.0

but I get (in 0.5-dev)

julia> r[2]
ERROR: InexactError()
 in indices at ./abstractarray.jl:45 [inlined]
 in checkbounds at ./abstractarray.jl:180 [inlined]
 in checkbounds at ./abstractarray.jl:194 [inlined]
 in getindex(::FloatRange{Float64}, ::Int64) at ./range.jl:461
 in eval(::Module, ::Any) at ./boot.jl:234
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:46

Is this intended, or is it just a bug? From the looks of it the error comes from somewhere in checkbounds, as the value of r[i] is simply (r.start + (i-1)*r.step)/r.divisor and it can be computed by hand without any errors.

@pwl
Copy link
Contributor Author

pwl commented Jul 24, 2016

Also, shouldn't pop! and similar functions be defined for Range? You could just modify the upper/lower bounds when removing elements from both ends.

@yuyichao
Copy link
Contributor

shouldn't pop! and similar functions be defined for Range

No, ranges are immutable.

@pwl
Copy link
Contributor Author

pwl commented Jul 24, 2016

No, ranges are immutable.

It makes sense now, I didn't know that.

@JeffBezanson
Copy link
Sponsor Member

I don't think Ranges are designed to support infinite length. I would say 1:Inf and 1:NaN should be errors.

@tkelman
Copy link
Contributor

tkelman commented Jul 26, 2016

we have countfrom for this, though it doesn't implement indexing

@pwl
Copy link
Contributor Author

pwl commented Jul 26, 2016

@JeffBezanson is that because 1:Inf has undefined length? Is there any indexible alternative to an infinite list? @tkelman mentioned countfrom, but it isn't indexible, same with repeated. I think having `getindex for these wouldn't hurt.

@mbauman
Copy link
Sponsor Member

mbauman commented Jan 27, 2017

I would say 1:Inf and 1:NaN should be errors.

Both now throw InexactErrors at construction time. Adding indexing to countfrom and repeated could be considered separately — see #15988 for an initial start.

@mbauman mbauman closed this as completed Jan 27, 2017
@tkelman
Copy link
Contributor

tkelman commented Jan 27, 2017

should add tests accordingly

@tkelman tkelman added the needs tests Unit tests are required for this change label Jan 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs tests Unit tests are required for this change
Projects
None yet
Development

No branches or pull requests

5 participants