-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
95 lines (72 loc) · 1.83 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
package sprec
import "math"
const (
Pi = float32(math.Pi)
Epsilon = float32(0.000001)
)
func Abs[T ~float32](value T) T {
return T(math.Float32frombits(math.Float32bits(float32(value)) &^ (1 << 31)))
}
func Max[T ~float32](a, b T) T {
return max(a, b)
}
func Min[T ~float32](a, b T) T {
return min(a, b)
}
func Floor[T ~float32](value T) T {
return T(math.Floor(float64(value)))
}
func Ceil[T ~float32](value T) T {
return T(math.Ceil(float64(value)))
}
func Clamp[T ~float32](value, lower, upper T) T {
return min(max(lower, value), upper)
}
func Mix[T ~float32](a, b T, amount float32) T {
return T(float32(a)*(1.0-amount) + float32(b)*amount)
}
func Eq(a, b float32) bool {
return EqEps(a, b, Epsilon)
}
func EqEps(a, b, epsilon float32) bool {
return Abs(a-b) < epsilon
}
func Mod(a, b float32) float32 {
return float32(math.Mod(float64(a), float64(b)))
}
func Sqrt(value float32) float32 {
return float32(math.Sqrt(float64(value)))
}
func Pow(a, b float32) float32 {
return float32(math.Pow(float64(a), float64(b)))
}
func Cos(angle Angle) float32 {
return float32(math.Cos(float64(angle.Radians())))
}
func Acos(cs float32) Angle {
return Radians(float32(math.Acos(float64(cs))))
}
func Sin(angle Angle) float32 {
return float32(math.Sin(float64(angle.Radians())))
}
func Asin(sn float32) Angle {
return Radians(float32(math.Asin(float64(sn))))
}
func Tan(angle Angle) float32 {
return float32(math.Tan(float64(angle.Radians())))
}
func Atan2(y, x float32) Angle {
return Radians(float32(math.Atan2(float64(y), float64(x))))
}
func Sign(value float32) float32 {
if math.Signbit(float64(value)) {
return -1.0
}
return 1.0
}
func IsNegative[T ~float32](value T) bool {
return math.Signbit(float64(value))
}
func IsValid[T ~float32](value T) bool {
return !math.IsNaN(float64(value)) && !math.IsInf(float64(value), 0)
}