-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
65 lines (57 loc) · 1.1 KB
/
misc.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
// Copyright (c) 2018, Mark "Happy-Ferret" Bauermeister
//
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package misc
import (
"fmt"
"math"
"runtime"
"strings"
)
func Max(a, b int) int {
if a > b {
return a
}
return b
}
func Min(a, b int) int {
if a < b {
return a
}
return b
}
// This exists because '%' isn't really modulus; it's *remainder*.
// e.g., (-1) % 2 = -1 but (-1) mod 2 = 1.
func Mod(x, m int) int {
r := x % m
if r < 0 {
r += m
}
return r
}
func Round(n float64) int {
ceil := math.Ceil(n)
if ceil-n <= 0.5 {
return int(ceil)
}
return int(ceil) - 1
}
// Prints a simple stack trace without panicing.
//
// XXX: I tried using runtime.Stack, but I couldn't get it to work...
func StackTrace() string {
// var pc uintptr
var fname string
var line int
var ok bool = true
lines := make([]string, 0, 10)
for i := 1; i < 200; i++ {
_, fname, line, ok = runtime.Caller(i)
if !ok {
break
}
lines = append(lines, fmt.Sprintf("%s:%d %s", fname, line, "N/A"))
}
return strings.Join(lines, "\n")
}