2023-09-20 15:14:38 +08:00
|
|
|
|
package number
|
2023-09-14 15:06:51 +08:00
|
|
|
|
|
2023-09-20 18:20:56 +08:00
|
|
|
|
import "math"
|
|
|
|
|
|
2023-09-14 15:06:51 +08:00
|
|
|
|
type Number interface {
|
|
|
|
|
~int | ~int8 | ~int16 | ~int32 | int64 |
|
|
|
|
|
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
|
|
|
|
|
~float32 | ~float64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsBetween n是否在a、b之间(等于也算)
|
|
|
|
|
func IsBetween[T Number](n T, a T, b T) bool {
|
|
|
|
|
if (n > a && n > b) || (n < a && n < b) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2023-09-20 15:14:38 +08:00
|
|
|
|
|
|
|
|
|
func Min[T Number](a T, b T) T {
|
|
|
|
|
if a <= b {
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Max[T Number](a T, b T) T {
|
|
|
|
|
if a >= b {
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
return b
|
|
|
|
|
}
|
2023-09-20 18:20:56 +08:00
|
|
|
|
|
|
|
|
|
func Abs[T Number](num T) T {
|
|
|
|
|
abs := math.Abs(float64(num))
|
|
|
|
|
return T(abs)
|
|
|
|
|
}
|