-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.go
45 lines (38 loc) · 831 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
package gomp
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 constraints.Signed | constraints.Float](x T) T {
if x < 0 {
return -x
}
return x
}
// Contains returns true if a value is available in the collection.
func Contains[T comparable](collection []T, value T) bool {
for _, v := range collection {
if v == value {
return true
}
}
return false
}