1
0
mirror of https://github.com/golang/go synced 2024-11-26 09:58:04 -07:00

Revert "math: add Compare and Compare32"

This reverts CL 467515. Now that we have cmp.Compare,
we don't need math.Compare or math.Compare32 after all.

For #56491
Fixes #60519

Change-Id: I8ed33464adfc6d69bd6b328edb26aa2ee3d234d9
Reviewed-on: https://go-review.googlesource.com/c/go/+/499416
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Eli Bendersky <eliben@google.com>
This commit is contained in:
Ian Lance Taylor 2023-05-30 15:19:44 -07:00 committed by Gopher Robot
parent e946f0e1ff
commit bf14663943
3 changed files with 0 additions and 149 deletions

View File

@ -1,2 +0,0 @@
pkg math, func Compare(float64, float64) int #56491
pkg math, func Compare32(float32, float32) int #56491

View File

@ -2094,67 +2094,6 @@ var sqrt32 = []float32{
-5.0106036182710749e+00, -5.0106036182710749e+00,
} }
type compareTest[F float32 | float64] struct {
x, y F
want int
}
func compareCasesFloat64() []compareTest[float64] {
zero, nan, inf := 0.0, NaN(), Inf(0)
// construct -NaN manually from its bit representation,
// since IEEE doesn't mandate negate(NaN) change the sign bit
unegnan := Float64bits(nan)
unegnan ^= 1 << 63
negnan := Float64frombits(unegnan)
return []compareTest[float64]{
{negnan, -inf, -1},
{-inf, negnan, 1},
{-inf, -Pi, -1},
{-Pi, -inf, 1},
{-Pi, -zero, -1},
{-zero, -Pi, 1},
{-zero, 0, -1},
{0, -zero, 1},
{0, Pi, -1},
{Pi, 0, 1},
{Pi, inf, -1},
{inf, Pi, 1},
{inf, nan, -1},
{nan, inf, 1},
{Pi, Pi, 0},
{negnan, negnan, 0},
}
}
func compareCasesFloat32() []compareTest[float32] {
zero, nan, inf := float32(0.0), float32(NaN()), float32(Inf(0))
// construct -NaN manually from its bit representation,
// since IEEE doesn't mandate negate(NaN) change the sign bit
unegnan := Float32bits(nan)
unegnan ^= 1 << 31
negnan := Float32frombits(unegnan)
return []compareTest[float32]{
{negnan, -inf, -1},
{-inf, negnan, 1},
{-inf, -Pi, -1},
{-Pi, -inf, 1},
{-Pi, -zero, -1},
{-zero, -Pi, 1},
{-zero, 0, -1},
{0, -zero, 1},
{0, Pi, -1},
{Pi, 0, 1},
{Pi, inf, -1},
{inf, Pi, 1},
{inf, nan, -1},
{nan, inf, 1},
{Pi, Pi, 0},
{negnan, negnan, 0},
}
}
func tolerance(a, b, e float64) bool { func tolerance(a, b, e float64) bool {
// Multiplying by e here can underflow denormal values to zero. // Multiplying by e here can underflow denormal values to zero.
// Check a==b so that at least if a and b are small and identical // Check a==b so that at least if a and b are small and identical
@ -2322,22 +2261,6 @@ func TestCeil(t *testing.T) {
} }
} }
func TestCompare(t *testing.T) {
// -NaN < -∞ < -3.14 < -0 < 0 < 3.14 < ∞ < NaN
for _, c := range compareCasesFloat64() {
cmp := Compare(c.x, c.y)
if cmp != c.want {
t.Errorf("Compare(%v, %v) = %d, want %v", c.x, c.y, cmp, c.want)
}
}
for _, c := range compareCasesFloat32() {
cmp := Compare32(c.x, c.y)
if cmp != c.want {
t.Errorf("Compare32(%v, %v) = %d, want %v", c.x, c.y, cmp, c.want)
}
}
}
func TestCopysign(t *testing.T) { func TestCopysign(t *testing.T) {
for i := 0; i < len(vf); i++ { for i := 0; i < len(vf); i++ {
if f := Copysign(vf[i], -1); copysign[i] != f { if f := Copysign(vf[i], -1); copysign[i] != f {
@ -3397,23 +3320,6 @@ func BenchmarkCeil(b *testing.B) {
GlobalF = x GlobalF = x
} }
func BenchmarkCompare(b *testing.B) {
x := 0
for i := 0; i < b.N; i++ {
x = Compare(GlobalF, 1.5)
}
GlobalI = x
}
func BenchmarkCompare32(b *testing.B) {
x := 0
globalF32 := float32(GlobalF)
for i := 0; i < b.N; i++ {
x = Compare32(globalF32, 1.5)
}
GlobalI = x
}
var copysignNeg = -1.0 var copysignNeg = -1.0
func BenchmarkCopysign(b *testing.B) { func BenchmarkCopysign(b *testing.B) {

View File

@ -1,53 +0,0 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package math
func sign[I int32 | int64](a, b I) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}
// Compare compares a and b such that
// -NaN is ordered before any other value,
// +NaN is ordered after any other value,
// and -0 is ordered before +0.
// In other words, it defines a total order over floats
// (according to the total-ordering predicate in IEEE-754, section 5.10).
// It returns 0 if a == b, -1 if a < b, and +1 if a > b.
func Compare(a, b float64) int {
// Perform a bitwise comparison (a < b) by casting the float64s into an int64s.
x := int64(Float64bits(a))
y := int64(Float64bits(b))
// If a and b are both negative, flip the comparison so that we check a > b.
if x < 0 && y < 0 {
return sign(y, x)
}
return sign(x, y)
}
// Compare32 compares a and b such that
// -NaN is ordered before any other value,
// +NaN is ordered after any other value,
// and -0 is ordered before +0.
// In other words, it defines a total order over floats
// (according to the total-ordering predicate in IEEE-754, section 5.10).
// It returns 0 if a == b, -1 if a < b, and +1 if a > b.
func Compare32(a, b float32) int {
// Perform a bitwise comparison (a < b) by casting the float32s into an int32s.
x := int32(Float32bits(a))
y := int32(Float32bits(b))
// If a and b are both negative, flip the comparison so that we check a > b.
if x < 0 && y < 0 {
return sign(y, x)
}
return sign(x, y)
}