rts-sim-module/util/number/number.go

37 lines
565 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package number
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
}
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
}
func Abs[T Number](num T) T {
if num < 0 {
return -num
}
return num
}