-
-
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
linrange
for constructing ranges by endpoints and length
#6627
Conversation
simonbyrne
commented
Apr 24, 2014
- Possibly relevant mailing list discussion
I should also mention that I'm open to better names for this. |
Just as a data point, a numpy-inspired syntax would be something like |
Using |
I don't like it too much myself. Just mentioning it for completeness. |
This should handle the |
Wasn't this what the |
|
Wait, |
Yes. Sometimes |
It occurs to me there are multiple ways this could be implemented: linrange1(a,b,len) = range(a, (b-a)/(len-1),len) # current implementation: 1.0 as the denominator
linrange2(a,b,len) = FloatRange(a*(len-1), b-a, float(len), float(len-1)) # use len-1 as the denominator
linrange3(a,b,len) = colon(a,(b-a)/(len-1),b) # use rationalisation julia> Base.showcompact(io::IO, x::Float64) = show(io,x)
julia> [linrange1(0.3,1.1,9) linrange2(0.3,1.1,9) linrange3(0.3,1.1,9)]
9x3 Array{Float64,2}:
0.3 0.3 0.3
0.4 0.4 0.4
0.5 0.5 0.5
0.6000000000000001 0.6000000000000001 0.6
0.7 0.7 0.7
0.8 0.8 0.8
0.9000000000000001 0.9000000000000001 0.9
1.0 1.0 1.0
1.1 1.1 1.1
# for reference:
julia> [linspace(0.3,1.1,9) float64(linspace(big(0.3),1.1,9))]
9x2 Array{Float64,2}:
0.3 0.3
0.4 0.4
0.5 0.5
0.6000000000000001 0.6
0.7000000000000001 0.7000000000000001
0.8 0.8
0.9 0.9
1.0000000000000002 1.0
1.1 1.1 |
Bump. I just ran into a case where I'd like to use this. With regards to the three possible implementations, it seems to me that there are a few properties that are essential:
So I did some very simplistic fuzz testing of these implementations. The other two implementations seem to be fairly comparable. They both break property 2 about 5% of the time, but are always within |
Thanks @mbauman. I've kept the current behaviour, as that seems the simplest, and added the handling for |
@@ -157,6 +157,8 @@ range(a::FloatingPoint, st::FloatingPoint, len::Integer) = FloatRange(a,st,len,o | |||
range(a::Real, st::FloatingPoint, len::Integer) = FloatRange(float(a), st, len, one(st)) | |||
range(a::FloatingPoint, st::Real, len::Integer) = FloatRange(a, float(st), len, one(a)) | |||
|
|||
linrange(a::Real,b::Real,len::Integer) = len >= 2 ? range(a, (b-a)/(len-1),len) : len == 1 && a == b ? range(a, zero(a), 1) : error("invalid range length") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use zero((b-a)/(len-1))
instead of zero(a)
.
This should be good to go: it would be great to have this in 0.3... |
`linrange` for constructing ranges by endpoints and length