2020-06-09 18:08:07 +01:00
|
|
|
package maths
|
|
|
|
|
|
|
|
// Abs gets the absolute value of an int
|
|
|
|
func Abs(x int) int {
|
|
|
|
if x < 0 {
|
|
|
|
return -x
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2020-06-30 23:59:58 +01:00
|
|
|
// Pmod is a mositive modulo
|
2020-06-09 18:08:07 +01:00
|
|
|
// golang's % is a "remainder" function si misbehaves for negative modulus inputs
|
|
|
|
func Pmod(x, d int) int {
|
|
|
|
if x == 0 || d == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
x = x % d
|
|
|
|
if x >= 0 {
|
|
|
|
return x
|
|
|
|
} else if d < 0 {
|
|
|
|
return x - d
|
|
|
|
} else {
|
|
|
|
return x + d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Max returns the highest int
|
2020-06-10 12:32:15 +01:00
|
|
|
func Max(x, y int) int {
|
2020-06-09 18:08:07 +01:00
|
|
|
if x < y {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
// Min returns the lowest int
|
2020-06-10 12:32:15 +01:00
|
|
|
func Min(x, y int) int {
|
2020-06-09 18:08:07 +01:00
|
|
|
if x > y {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|