gob/number/util.go

53 lines
784 B
Go

package number
import (
"math/big"
"gitea.zaclys.com/bvaudour/gob/option"
)
type integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
type float interface {
~float32 | ~float64
}
type rat = option.Option[*big.Rat]
func signOf[N integer | float](n N) int {
switch {
case n < 0:
return -1
case n > 0:
return 1
default:
return 0
}
}
func abs[N integer | float](n N) N {
if n < 0 {
return -n
}
return n
}
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
}