Sander Schobers
73c6048f09
- Added some statistical (related to summing and countings digits) and math methods (related to powers and square roots).
116 lines
1.5 KiB
Go
116 lines
1.5 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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)))
|
|
}
|