summaryrefslogtreecommitdiff
path: root/src/math/compare.go
blob: 37981100727e171e0657ceaad83f1fcd50b30a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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)
}