geom/ints/math.go

142 lines
2.0 KiB
Go

package ints
import "math"
// Abs returns the absolute value of i.
func Abs(i int) int {
if 0 > i {
return -i
}
return i
}
// Abs64 returns the absolute value of i.
func Abs64(i int64) int64 {
if 0 > i {
return -i
}
return i
}
// Min returns the minimum of a and b.
func Min(a, b int) int {
if a < b {
return a
}
return b
}
// Min64 returns the minimum of a and b.
func Min64(a, b int64) int64 {
if a < b {
return a
}
return b
}
// Max returns the maximum of a and b.
func Max(a, b int) int {
if a > b {
return a
}
return b
}
// Max64 returns the maximum of a and b.
func Max64(a, b int64) int64 {
if a > b {
return a
}
return b
}
// Norm returns the normalized value of i (-1, 0 or 1).
func Norm(i int) int {
if i < 0 {
return -1
}
if i > 0 {
return 1
}
return 0
}
// Norm64 returns the normalized value of i (-1, 0 or 1).
func Norm64(i int64) int64 {
if i < 0 {
return -1
}
if i > 0 {
return 1
}
return 0
}
// NthRoot returns the nth root of i.
func NthRoot(i, n int) int {
return int(math.Pow(float64(i), 1/float64(n)))
}
// NthRoot64 returns the nth root of i.
func NthRoot64(i, n int64) int64 {
return int64(math.Pow(float64(i), 1/float64(n)))
}
// SubAbs subtracts a from b and returns the absolute value of the result.
func SubAbs(a, b int) int {
if a > b {
return a - b
}
return b - a
}
// SubAbs64 subtracts a from b and returns the absolute value of the result.
func SubAbs64(a, b int64) int64 {
if a > b {
return a - b
}
return b - a
}
// Pow returns the power of base b to the exponent e.
func Pow(b, e int) int {
if 0 == e {
return 1
}
if 1 == e {
return b
}
p := b
for e > 1 {
p *= b
e--
}
return p
}
// Pow64 returns the power of base b to the exponent e.
func Pow64(b, e int64) int64 {
if 0 == e {
return 1
}
if 1 == e {
return b
}
p := b
for e > 1 {
p *= b
e--
}
return p
}
// Sqrt returns square root of n.
func Sqrt(n int) int {
return int(math.Sqrt(float64(n)))
}
// Sqrt64 returns square root of n.
func Sqrt64(n int64) int64 {
return int64(math.Sqrt(float64(n)))
}