-
Notifications
You must be signed in to change notification settings - Fork 1
/
math.go
77 lines (62 loc) · 968 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package utils
import "math"
func Abs(n int) int {
if n < 0 {
return -n
}
return n
}
func Abs64(n int64) int64 {
if n < 0 {
return -n
}
return n
}
/*
Sum returns the sum of the given slice of ints
*/
func Sum(s []int) int {
var x int
for i := range s {
x += s[i]
}
return x
}
func FloorInt(f float64) int {
return int(math.Floor(f))
}
func CeilInt(f float64) int {
return int(math.Ceil(f))
}
// Round returns the rounded value of a float64 up to N places
func Round(val float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= 0.5 {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
func MinInt(a, b int) int {
if a < b {
return a
}
return b
}
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
func MaxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}