-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmath.go
53 lines (45 loc) · 927 Bytes
/
math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gogu
import "golang.org/x/exp/constraints"
// Min returns the lowest value from the provided parameters.
func Min[T constraints.Ordered](values ...T) T {
var acc T = values[0]
for _, v := range values {
if v < acc {
acc = v
}
}
return acc
}
// Max returns the biggest value from the provided parameters.
func Max[T constraints.Ordered](values ...T) T {
var acc T = values[0]
for _, v := range values {
if v > acc {
acc = v
}
}
return acc
}
// Abs returns the absolut value of x.
func Abs[T Number](x T) T {
if x < 0 {
return -x
}
return x
}
// Clamp returns a range-limited number between min and max.
func Clamp[T Number](num, min, max T) T {
if num <= min {
return min
} else if num >= max {
return max
}
return num
}
// InRange checks if a number is inside a range.
func InRange[T Number](num, lo, up T) bool {
if num >= lo && num <= up {
return true
}
return false
}