-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
In #56491 we added math.Compare. The original motivation was:
The
slices.Sortdoesn't support sorting slices of floating-point numbers containing Not-a-number (NaN) values, It's not very intuitive.Once the
slicespackage is merged into stdlib, thesort.{TYPE}s(e.g.sort.Ints,sort.Float64s) APIs may be semi-deprecated. But theslicespackage doesn't provide a simple way to sort slices of floats containing NaNs, it recommends usingslices.SortFunc(x, func(a, b float64) bool {return a < b || (math.IsNaN(a) && !math.IsNaN(b))}), this function is very complex compared to usingsort.Float64s.
This need is addressed not only by math.Compare but also by the recently added cmp.Compare.
We ended up with different semantics with respect to NaNs for cmp.Compare and math.Compare. For cmp.Compare we order NaNs all less than negative infinity, the same as sort.Float64s always has. For math.Compare we used the total order predicate from IEEE 754, which sorts NaNs with a negative sign less than negative infinity and NaNs with a positive sign greater than positive infinity. This is strange because NaNs don't even have signs technically, and there's no easy way to get at the sign.
Given that
(1) no one was asking for IEEE 754 NaN total ordering, and
(2) cmp.Compare now addresses the original motivation for math.Compare,
I believe we should remove math.Compare and math.Compare32.
We can always add them back in a future release if someone really does need the IEEE 754 semantics, but we don't have evidence for that today.