gob/number/util.go

37 lines
605 B
Go
Raw Normal View History

2024-02-17 18:13:24 +00:00
package number
type integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
type float interface {
~float32 | ~float64
}
func signOf[N integer | float](n N) int {
switch {
case n < 0:
return -1
case n > 0:
return 1
default:
return 0
}
}
func compare[N integer | float](n1, n2 N) int {
return signOf(n1 - n2)
}
func isBaseValid[N integer | float](base N) bool {
return base >= 0 && base <= 36
}
func formatBase[N integer | float](base ...N) uint {
if len(base) > 0 && isBaseValid(base[0]) {
return uint(base[0])
}
return 10
}