-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
50 lines (45 loc) · 949 Bytes
/
int.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
package gomath
// MaxInt takes in two or more integers and returns
// the maximum of them
func MaxInt(a int, rem ...int) int {
res := a
for _, v := range rem {
if v > res {
res = v
}
}
return res
}
// MinInt takes in two or more integers and returns
// the maximum of them
func MinInt(a int, rem ...int) int {
res := a
for _, v := range rem {
if v < res {
res = v
}
}
return res
}
// AbsInt takes an integer and returns its absolute
// value
func AbsInt(a int) int {
return MaxInt(a, -a)
}
// ClampInt will clamp a given value between a low and
// high value inclusively.
func ClampInt(low, high, val int) (result int) {
if val < low {
result = low
} else if val > high {
result = high
} else {
result = val
}
return
}
// ScaleInt will scale a number from the old range
// to the new range
func ScaleInt(low, high, oldLow, oldHigh, value int) int {
return low + (high-low)*((value-oldLow)/(oldHigh-oldLow))
}