-
-
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
Indexing a range by a range #7420
Comments
The first example is what I would have expected. On Julia 0.2.1 the second example gives Which version are you using? |
In the first example, look carefully and compare I am using one of the latest nightly releases, currently 0.3.0-prerelease+3882 |
Oops, note to self, read carefully :) Example 1 r[1:5] is however also correct on 0.2.1 so its a regression. |
When indexing a FloatRange with a OrdinalRange, you have to be careful to use the actual r.step, instead of step(r) = r.step/r.divisor.
I think this was an easy fix (see: #7421) |
@StefanKarpinski There are more occurrences of similar errors: julia> a = 0.1:0.1:1
julia> [reverse(a)] == reverse([a])
false Culprint in the use of |
Related to JuliaLang#7420
Related to JuliaLang#7420 Added test for reverse(r::FloatRange)
I'm using my newly acquired skills to Reopen this issue with reference to x = linrange(1,10,20)
(x[2:end][end] == 10.0) == false Thanks to @simonp0420 for noting my typo. |
I would have hoped that function getindex(r::FloatRange, s::OrdinalRange)
if isempty(s)
return FloatRange(1.0, 1.0, 0.0, 1.0)
end
1 <= first(s) <= length(r) &&
1 <= last(s) <= length(r) || throw(BoundsError())
linrange(r[first(s)], r[last(s)], length(s))
end Maybe indexing of a |
Would it make any sense for FloatRange objects to store the endpoints and length, rather than start, step, and length? |
No |
Agreed, that would be a very bad idea. Nevertheless, it seems that Julia is giving the initial value of a FloatRange a very distinguished role, since its value is preserved exactly. It seems to me that the final value should be equally distinguished, with the understanding that the intermediate values might be approximated due to floating point issues. Would it make any sense to add the final point to the list of values stored in a FloatRange and to somehow provide this stored value whenever the final range value is requested? I suppose this would drastically degrade efficiency... |
Background reading for |
I'll take a look at this when I can. |
Thanks, @pao. A lot of thought and work has gone into ranges. I guess that introducing the new linrange function has added yet another wrinkle to consider. |
Thinking more about this, we only
Edited to add |
I am asking just out of interest; my apologies if this is not the right place. I know very little about the issues related to FloatingPoint precision. Why is it not possible to efficiently compute intermediate points from is I guess one could also add two As stated at the beginning, these are not actual suggestions, since I am unexperienced with all the complications involved. Just asking out of curiosity... On 02 Jul 2014, at 18:44, Ivar Nesje notifications@github.com wrote:
|
It seems important that indexing a FloatRange by an OrdinalRange gives exactly the same values as indexing by the corresponding Vector{Int}, but returning a Vector seems a little unpleasant. Maybe we could return a SubArray? We could even define |
What would make |
It wouldn't allocate memory, which is one of the main advantages of the FloatRange type. (It is basically the wrapper type that performs linear transformations on indices that you suggest in [3] above.) In principle it could also be faster to use. I'm not sure if that's true at present, but hopefully we'll get faster array views in 0.4. |
So that would be more a |
Rather than making a new type for this, one could also add fields |
@Jutho That was what I intended to suggest in [1], but i thought we could just store |
Is there anything really 0.3 blocking here? I think the enhancements to FloatRange can be further explored in 0.4. |
I don't think it is blocking If consensus / benchmarks takes time, I think that we should just return an array for now, and explore further in 0.4 |
The issue here is that julia> r = FloatRange{Float64}(19,9,20,19)
1.0:0.47368421052631576:10.0
julia> r[end]
10.0
julia> r[2:end]
1.4736842105263157:0.47368421052631576:10.0
julia> r[2:end][end]
10.0 Now I just need to work out how to make sure that's the construction that happens. |
Stefan and I discussed that |
That type of formula has a compelling symmetry. But isn't it |
All the examples in this issue seem to be working now, due to #9666 and its follow-on work. Is there anything left to do here? |
Bump. Yes, looks like everything here is working now. Plz reopen if not. |
I don't think this is expected behaviour?
From doing more tests, the behaviour is very weird. It looks as if for all
r
with step size <= 0.25, the starting point and step size are first being multiplied by the step size before the indexing happens; in particular the new step size is the square of the old one: e.g.The text was updated successfully, but these errors were encountered: