-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
124 lines (106 loc) · 1.94 KB
/
util.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Package util provides miscellaneous maths functions.
package util
import (
"fmt"
"math"
"math/rand"
)
func Max(x, y float64) float64 {
var m float64
if x > y {
m = x
} else {
m = y
}
return m
}
func Max3(x, y, z float64) float64 {
max := x
if y > max {
max = y
}
if z > max {
max = z
}
return max
}
func Min(x, y float64) float64 {
var m float64
if x < y {
m = x
} else {
m = y
}
return m
}
func Re(x float64) float64 {
return Max(x, 0)
}
// Frac returns x % 1, using python-like modulus.
func Frac(x float64) float64 {
return x - math.Floor(x)
}
func Dither(rng *rand.Rand, x float64) int {
i, f := math.Modf(x)
if rng.Float64() < f {
i++
}
return int(i)
}
func Sqr(x float64) float64 {
return x * x
}
func Assert(test bool) {
if !test {
panic("assertion failed")
}
}
func IsBad(v float64) bool {
return math.IsNaN(v) || math.IsInf(v, 1) || math.IsInf(v, -1)
}
func IsBadVec(v [3]float64) bool {
return IsBad(v[0]) || IsBad(v[1]) || IsBad(v[2])
}
func CheckNaN(v float64) {
if IsBad(v) {
panic(fmt.Sprint("got bad number:", v))
}
}
func CheckNaNVec(v [3]float64) {
if math.IsNaN(v[0]) || math.IsNaN(v[1]) || math.IsNaN(v[2]) {
panic(fmt.Sprint("got NaN:", v))
}
}
// Sin is like math.Sin, but returns exactly 0 for arguments Pi, -Pi.
// this avoids annoying round-off errors for rotations of multiples of 90 degrees.
func Sin(x float64) float64 {
switch x {
default:
return math.Sin(x)
case pi, -pi:
return 0
}
}
// Cos is like math.Cos, but returns exactly 0 for arguments Pi/2, -Pi/2.
// this avoids annoying round-off errors for rotations of multiples of 90 degrees.
func Cos(x float64) float64 {
switch x {
default:
return math.Cos(x)
case pi / 2, -pi / 2:
return 0
}
}
// Sinc returns sin(x)/x, handling x == 0 gracefully.
func Sinc(x float64) float64 {
if x == 0 {
return 1
}
return math.Sin(x) / x
}
const pi = math.Pi
func Check(err error) {
if err != nil {
panic(err)
}
}