1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 func sign[I int32 | int64](a, b I) int {
17 // Compare compares a and b such that
18 // -NaN is ordered before any other value,
19 // +NaN is ordered after any other value,
20 // and -0 is ordered before +0.
21 // In other words, it defines a total order over floats
22 // (according to the total-ordering predicate in IEEE-754, section 5.10).
23 // It returns 0 if a == b, -1 if a < b, and +1 if a > b.
24 func Compare(a, b float64) int {
25 // Perform a bitwise comparison (a < b) by casting the float64s into an int64s.
26 x := int64(Float64bits(a))
27 y := int64(Float64bits(b))
29 // If a and b are both negative, flip the comparison so that we check a > b.
36 // Compare32 compares a and b such that
37 // -NaN is ordered before any other value,
38 // +NaN is ordered after any other value,
39 // and -0 is ordered before +0.
40 // In other words, it defines a total order over floats
41 // (according to the total-ordering predicate in IEEE-754, section 5.10).
42 // It returns 0 if a == b, -1 if a < b, and +1 if a > b.
43 func Compare32(a, b float32) int {
44 // Perform a bitwise comparison (a < b) by casting the float32s into an int32s.
45 x := int32(Float32bits(a))
46 y := int32(Float32bits(b))
48 // If a and b are both negative, flip the comparison so that we check a > b.