forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
50 lines (43 loc) · 893 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
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package utils
import (
"github.com/pingcap/log"
"go.uber.org/zap"
)
// MinInt choice smallest integer from its arguments.
func MinInt(x int, xs ...int) int {
min := x
for _, n := range xs {
if n < min {
min = n
}
}
return min
}
// MaxInt choice biggest integer from its arguments.
func MaxInt(x int, xs ...int) int {
max := x
for _, n := range xs {
if n > max {
max = n
}
}
return max
}
// ClampInt restrict a value to a certain interval.
func ClampInt(n, min, max int) int {
if min > max {
log.Error("clamping integer with min > max", zap.Int("min", min), zap.Int("max", max))
}
return MinInt(max, MaxInt(min, n))
}
// MinInt64 choice smallest integer from its arguments.
func MinInt64(x int64, xs ...int64) int64 {
min := x
for _, n := range xs {
if n < min {
min = n
}
}
return min
}