-
-
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 min/max
use isless
even for Real
#51356
base: master
Are you sure you want to change the base?
Conversation
In #23155, we made the generic `min/max` use `isless` rather than `<`. This is motivated by the observation that the implementation min(a, b) = cmp(a, b) ? a : b implies that `cmp` satisfies the same properties as `isless` (either `cmp(a, b)` in which case we return `a`, otherwise either `cmp(b, a)` or they are equal, in which case returning `b` is correct). However, this is not true for `Real` types, including AbstractFloat (due to `NaN`). Part of the motivation here is that many packages seem to subtype `Real` when, theoretically, perhaps they shouldn't. Prominent examples include `IntervalArithmetic.Interval` and `ForwardDiff.Dual`.
@nanosoldier |
The package evaluation job you requested has completed - possible new issues were detected. |
Does this mean I wonder whether with this change we could get rid of the "bad zero" shenanigans in Line 637 in 53a00f3
|
@nanosoldier |
There are two methods higher in the priority list: julia> methods(min, Tuple{T,T} where T<:Real)
# 3 methods for generic function "min" from Base:
[1] min(x::T, y::T) where T<:Union{Float32, Float64}
@ Base.Math math.jl:894
[2] min(x::T, y::T) where T<:AbstractFloat
@ Base.Math math.jl:878
[3] min(x::T, y::T) where T<:Real
@ promotion.jl:533 That said, you raise an interesting point. TBH I'm not really an expert on the nuances of In other words, the way that I'd like for this to work is to implement |
The package evaluation job you requested has completed - possible new issues were detected. |
In #23155, we made the generic
min/max
useisless
rather than<
. This is motivated by the observation that the implementationimplies that
cmp
satisfies the same properties asisless
(eithercmp(a, b)
in which case we returna
, otherwise eithercmp(b, a)
or they are equal, in which case returningb
is correct).However, this is not true for
Real
types, including AbstractFloat (due toNaN
). Yet currently the default forReal
uses<
.Part of the motivation here is that many packages seem to subtype
Real
when, theoretically, perhaps they shouldn't. Prominent examples includeIntervalArithmetic.Interval
andForwardDiff.Dual
. An example using intervals that would fail is the following:min(1..3, 2..4)
: the two intervals have non-empty intersection, so it's not/shouldn't be true that1..3 < 2..4
, in which case2..4
would be returned (which is wrong by any measure).To be honest, I don't expect this to be mergeable, as I bet it is breaking. But I think it's worth seeing if it passes tests (I haven't tried locally) and perhaps a PkgEval run.