-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
math.go
119 lines (98 loc) · 2.54 KB
/
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
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
package gfx
import "math"
// Mathematical constants.
const (
Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
)
// MathMin returns the smaller of x or y.
func MathMin(x, y float64) float64 {
return math.Min(x, y)
}
// MathMax returns the larger of x or y.
func MathMax(x, y float64) float64 {
return math.Max(x, y)
}
// MathAbs returns the absolute value of x.
func MathAbs(x float64) float64 {
return math.Abs(x)
}
// MathSqrt returns the square root of x.
func MathSqrt(x float64) float64 {
return math.Sqrt(x)
}
// MathSin returns the sine of the radian argument x.
func MathSin(x float64) float64 {
return math.Sin(x)
}
// MathSinh returns the hyperbolic sine of x.
func MathSinh(x float64) float64 {
return math.Sinh(x)
}
// MathCos returns the cosine of the radian argument x.
func MathCos(x float64) float64 {
return math.Cos(x)
}
// MathCosh returns the hyperbolic cosine of x.
func MathCosh(x float64) float64 {
return math.Cosh(x)
}
// MathAtan returns the arctangent, in radians, of x.
func MathAtan(x float64) float64 {
return math.Atan(x)
}
// MathTan returns the tangent of the radian argument x.
func MathTan(x float64) float64 {
return math.Tan(x)
}
// MathCeil returns the least integer value greater than or equal to x.
func MathCeil(x float64) float64 {
return math.Ceil(x)
}
// MathFloor returns the greatest integer value less than or equal to x.
func MathFloor(x float64) float64 {
return math.Floor(x)
}
// MathHypot returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow.
func MathHypot(p, q float64) float64 {
return math.Hypot(p, q)
}
// MathPow returns x**y, the base-x exponential of y.
func MathPow(x, y float64) float64 {
return math.Pow(x, y)
}
// MathLog returns the natural logarithm of x.
func MathLog(x float64) float64 {
return math.Log(x)
}
// MathRound returns the nearest integer, rounding half away from zero.
func MathRound(x float64) float64 {
return math.Round(x)
}
// Sign returns -1 for values < 0, 0 for 0, and 1 for values > 0.
func Sign(x float64) float64 {
switch {
case x < 0:
return -1
case x > 0:
return 1
default:
return 0
}
}
// Clamp returns x clamped to the interval [min, max].
//
// If x is less than min, min is returned. If x is more than max, max is returned. Otherwise, x is
// returned.
func Clamp(x, min, max float64) float64 {
if x < min {
return min
}
if x > max {
return max
}
return x
}
// Lerp does linear interpolation between two values.
func Lerp(a, b, t float64) float64 {
return a + (b-a)*t
}