2019-12-21 11:48:57 +00:00
|
|
|
package ints
|
|
|
|
|
2019-12-21 12:01:47 +00:00
|
|
|
import "math"
|
|
|
|
|
2019-12-21 11:48:57 +00:00
|
|
|
// Abs returns the absolute value of i.
|
|
|
|
func Abs(i int) int {
|
|
|
|
if 0 > i {
|
|
|
|
return -i
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2020-06-07 06:44:34 +00:00
|
|
|
// Abs64 returns the absolute value of i.
|
|
|
|
func Abs64(i int64) int64 {
|
|
|
|
if 0 > i {
|
|
|
|
return -i
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2019-12-21 11:48:57 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2019-12-21 12:01:47 +00:00
|
|
|
|
2020-06-07 06:44:34 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-12-21 12:01:47 +00:00
|
|
|
// 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)))
|
|
|
|
}
|