Skip to content

math: remove Compare, Compare32 #60519

@rsc

Description

@rsc

In #56491 we added math.Compare. The original motivation was:

The slices.Sort doesn't support sorting slices of floating-point numbers containing Not-a-number (NaN) values, It's not very intuitive.

Once the slices package is merged into stdlib, the sort.{TYPE}s(e.g. sort.Ints, sort.Float64s) APIs may be semi-deprecated. But the slices package doesn't provide a simple way to sort slices of floats containing NaNs, it recommends using slices.SortFunc(x, func(a, b float64) bool {return a < b || (math.IsNaN(a) && !math.IsNaN(b))}), this function is very complex compared to using sort.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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions