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
|
//go:build !go1.21
package xmath
import "golang.org/x/exp/constraints"
// Min returns the minimum of a and b based on the < operator.
func Min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
// Max returns the maximum of a and b based on the > operator.
func Max[T constraints.Ordered](a, b T) T {
if a > b {
return a
}
return b
}
// Clamp clamps the value of x to within min and max.
func Clamp[T constraints.Ordered](x, min, max T) T {
if x < min {
return min
}
if x > max {
return max
}
return x
}
|