-
-
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
Make StepRange safe for more number types #17611
Conversation
CC @quinnj for the changes to the Dates tests. OK? |
I very much appreciate your efforts with this. I'm so glad to see these issues receiving attention. Might I suggest Edit: okay maybe |
If something is an I'm also uneasy with the notion of |
They are generated by the additive group generator. Let's not confuse that with multiplicative identity, even when it happens to work for integers without units. I 100% endorse the proposal for an additive group generator by @StefanKarpinski in #16116. If I had that function to play with, units would work so much better. |
I came to the conclusion earlier that so long as promotion doesn't automatically strip or add units, I'm okay with To be clear, there is another syntax in Unitful which is more questionable, To summarize,
I'm quite happy with the first two but would understand objections to the last two. |
julia> convert(typeof(1km), 1s)
ERROR: Dimensional mismatch.
in error(::String) at ./error.jl:21
in convert(...) at /home/tim/.julia/v0.5/Unitful/src/Unitful.jl:926
in convert(...) at /home/tim/.julia/v0.5/Unitful/src/Unitful.jl:1004
in eval(::Module, ::Any) at ./boot.jl:234
in macro expansion at ./REPL.jl:92 [inlined]
in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:46
julia> convert(typeof(1km), convert(typeof(1), 1s))
1 km So it works differently if you first convert to some other intermediary. I can imagine similar behavior emerging by accident (e.g., if |
I have to say that using |
I hope we can isolate discussion about |
No arguments here that |
Our poor CI seems to be having a rough day. AFAICT none of those failures are relevant. |
Since we're in feature-freeze, I'll let someone else decide about merging this. |
last | ||
end | ||
# For types where x+one(x) may not be well-defined | ||
steprange_last_empty(start, step, stop) = start - step |
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.
Wouldn't this be a more general way of handling overflow issues without using one
or zero
?
function steprange_last_empty(start, step, stop)
prev = start - step
last = prev < start ? prev : start + step
return last
end
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.
As far as the implementation of this function goes, if you get here, you already know that step
is in the opposite direction of zero
as that between start
and stop
---see line 33 above. So if you make use of step
, I don't think you need the branch. But please check!
Or are you proposing this logic as a more general way to detect that the range is empty, without needing even zero
?
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.
You are correct that you don't need the extra branch. May be worth adding a comment to the function.
Or are you proposing this logic as a more general way to detect that the range is empty, without needing even
zero
?
I think the code using zero
is cleaner and probably more performant.
Changes to the tests look fine by me. |
Has it been decided that this isn't 0.5 material? I think you can basically regard this as a bugfix, for when |
This seems reasonable and hopefully low-risk to me, but I'd want to keep an eye on packageevaluator just in case (it's having some freezing trouble due to unreliable downloads that I'm working on fixing right now, so hasn't run to completion in a couple days). |
Thanks. How about we merge once you've got packageevaluator working again. |
curlrc to the rescue, the currently executing run looks like it should finish properly. |
@@ -14,7 +14,7 @@ function test_all_combos() | |||
@test length(dr) == 0 | |||
@test isempty(dr) | |||
@test first(dr) == f1 | |||
@test last(dr) == f1-one(l1 - f1) | |||
@test last(dr) < f1 |
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.
instead of one
, should these stay equality tests but use pos_step
?
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.
I'd argue that the specific choices in how an empty range is implemented is an internal detail that we actively want to avoid testing.
Good to go here? |
Currently
StepRange
is the only range type in Base that's potentially usable for objects wherex+one(x)
may not be definable. This relaxes what I suspect to be an unnecessary constraint---wraparound only seems relevant forInteger
types.