-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
The result of an empty intersection of ranges is confusing #40331
Comments
Maybe we could print empty ranges differently, adding a hint like the following: julia> 5:4
5:4 (empty UnitRange{Int}) |
There should be a better way to show an empty range then |
Commonly, we summarize by printing the length of an array. Might we useful always?
|
this issue should definitely be addressed somehow. For all things (sets) empty probably the best should be to print julia > intersect(a,b)
∅ or similar to [by @sostock ] julia > intersect(a,b)
empty UnitRange{Int} without any ranges left in the printing, which are really confusing. |
For ranges, the start/stop values sometimes carry meaning even for empty ranges. From the julia> searchsorted([1, 2, 4, 5, 5, 7], 3) # no match, insert in the middle
3:2
julia> searchsorted([1, 2, 4, 5, 5, 7], 9) # no match, insert at end
7:6 Here, not printing the values for empty ranges would hide where the number would be inserted. Therefore, I think that the start/stop values should always be printed for ranges, even when they are empty. |
the problem is that the output looks like the actual value output, when in practice one needs the emptyness of that operation. If you want to keep that information for whatever reason, then it should appear after the actual value output, e.g., julia> searchsorted([1, 2, 4, 5, 5, 7], 3)
∅ (empty 3:2) |
To try out always printing the length, as other arrays do: julia> function Base.show(io::IO, ::MIME"text/plain", r::AbstractRange)
summary(io, r); print(io, ":\n "); show(io, r)
end
julia> intersect(1:3, 5:6)
0-element UnitRange{Int64}:
5:4
julia> 1:2:3
2-element StepRange{Int64, Int64}:
1:2:3
julia> LinRange(1,2,3) # unchanged
3-element LinRange{Float64, Int64}:
1.0, 1.5, 2.0
julia> range(1,2,3) # quite long
3-element StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}:
1.0:0.5:2.0 |
Triage thinks the first answer proposed was the right one.
|
Maybe just |
As mentioned in #40331, empty ranges could be confusing. This pr adds a message "(empty range)" incase if the range is empty. So for the example given in the issue ```sh julia> a = 1:3 1:3 julia> b = 5:6 5:6 julia> intersect(a,b) # old behaviour 5:4 julia> intersect(a,b) # new behaviour 5:4 (empty range) ```
I think we can close this now? |
If you call
intersect
between two ranges and the result is empty it returns an empty range of the form5:4
as in likeThat can be confusing since you might interpret
5:4
as5:-1:4
Using Float ranges gives a different result though
The text was updated successfully, but these errors were encountered: