-
-
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
specialize argmin/argmax on ranges #34315
Conversation
merge? |
This is not something to care about (will not break any actual code), but it may be worth mentioning that julia> argmax(9.999999999e15:1e16)
1000000
julia> lastindex(9.999999999e15:1e16)
1000001 Nevertheless I think this specialization makes sense. It might be worth documenting that the "If there are multiple maximal elements, then the first one will be returned." does not necessarily apply to certain pathological ranges, but then again, perhaps this case is so rare that it's unimportant. (Or, maybe revise the current argmax documentation to say "For |
Urgs, I think it is nice that |
Here is an example of a degenerate range, that I can imagine happening in real code: julia> r = map(Float32, 1:10^8)
1.0f0:1.0f0:1.0f8
julia> collect(r[end-10:end])
11-element Array{Float32,1}:
9.999999e7
9.999999e7
9.999999e7
9.999999e7
9.999999e7
1.0e8
1.0e8
1.0e8
1.0e8
1.0e8
1.0e8
|
Here is a related bug: julia> searchsorted(1f0:1f8, 1f8)
100000000:100000000
julia> (1f0:1f8)[end-1]
1.0f8 |
function argmin(r::AbstractRange)
if isempty(r)
throw(ArgumentError("range must be non-empty"))
elseif step(r) > 0
firstindex(r)
else
first(searchsorted(r, last(r)))
end
end This should be the solution if there was not #34408 . I don't feel like fixing #34408 however, at least not any time soon. So I documented, that a non first index may be returned. |
I think CI fail is unrelated to the PR. |
Can you rebase on a newer master? That's a lot of test failures, which makes me nervous. |
Co-Authored-By: Stefan Karpinski <stefan@karpinski.org>
Can somebody rerun the failing CI? |
That particular CI failure has been happening lately and is unrelated. Very unlikely that this is failing only on FreeBSD, so I just merged since there were no other failures there or elsewhere. |
before:
after: