/
math.go
214 lines (178 loc) · 4.18 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package math
import (
"fmt"
"math"
"math/rand"
"time"
)
// Abs returns the value of the absolute value of a number
func Abs(x float64) float64 {
if x < 0 {
return -x
}
return x
}
// Acos returns the value of the arc cosine of a number
func Acos(x float64) float64 {
return math.Acos(x)
}
// Acosh returns the value of the arc hyperbolic cosine of a number
func Acosh(x float64) float64 {
return math.Acosh(x)
}
// Asin returns the value of the arc sine of a number
func Asin(x float64) float64 {
return math.Asin(x)
}
// Asinh returns the value of the arc hyperbolic sine of a number
func Asinh(x float64) float64 {
return math.Asinh(x)
}
// Atan returns the value of the arc tangent of a number
func Atan(x float64) float64 {
return math.Atan(x)
}
// Atanh returns the value of the arc hyperbolic tangent of a number
func Atanh(x float64) float64 {
return math.Atanh(x)
}
// Cbrt returns the value of the cube root of a number
func Cbrt(x float64) float64 {
return math.Cbrt(x)
}
// Ceil returns the value of the ceiling of a number
func Ceil(x float64) float64 {
var xi int
xi = int(x)
if x > float64(xi) {
return float64(xi + 1)
}
return float64(xi)
}
// Cos returns the value of cosinus of an angle
func Cos(x float64) float64 {
return math.Cos(x)
}
// Cosh returns the value of the hyperbolic cosine of a number
func Cosh(x float64) float64 {
return math.Cosh(x)
}
// DegreesToRadians converts degrees to radians.
func DegreesToRadians(x float64) float64 {
return x * (math.Pi / 180.0)
}
// Exp returns the value of the exponential of a number
func Exp(x float64) float64 {
return math.Exp(x)
}
// Fact returns the value of the factorial of a number
func Fact(x float64) float64 {
if x < 0 {
return 0
}
if x == 0 {
return 1
}
result := float64(1)
for i := float64(2); i <= x; i++ {
result *= i
}
return result
}
// Floor returns the value of the floor of a number
func Floor(x float64) float64 {
var xi int
xi = int(x)
if x < float64(xi) {
return float64(xi - 1)
}
return float64(xi)
}
// Ln returns the value of the natural logarithm of a number
func Ln(x float64) float64 {
return math.Log(x)
}
// Log10 returns the base-10 logarithm of a number.
func Log10(x float64) float64 {
return math.Log10(x)
}
// Max returns the value of the maximum of two numbers
func Max(x float64, y float64) float64 {
if x >= y {
return x
}
return y
}
// Min returns the value of the minimum of two numbers
func Min(x float64, y float64) float64 {
if y <= x {
return y
}
return x
}
// Modulo returns the remainder of a division operation.
func Modulo(x, y float64) float64 {
return math.Mod(x, y)
}
// Pi returns the value of PI
func Pi() float64 {
return math.Pi
}
// Pow returns the value of the power of a number
func Pow(x any, y any) (float64, error) {
var xFloat, yFloat float64
switch typeX := x.(type) {
case int:
xFloat = float64(typeX)
case float64:
xFloat = typeX
default:
return 0, fmt.Errorf("invalid type")
}
switch typeY := y.(type) {
case int:
yFloat = float64(typeY)
case float64:
yFloat = typeY
default:
return 0, fmt.Errorf("invalid type")
}
return math.Pow(xFloat, yFloat), nil
}
// RadiansToDegrees converts radians to degrees.
func RadiansToDegrees(x float64) float64 {
return x * (180.0 / math.Pi)
}
// Random generates a random number between a given range.
func Random(x, y float64) float64 {
source := rand.New(rand.NewSource(time.Now().UnixNano()))
return source.Float64()*(y-x) + x
}
// Round rounds a number to the nearest integer.
func Round(x float64) float64 {
return math.Round(x)
}
// Sin returns the value of sinus of an angle
func Sin(x float64) float64 {
return math.Sin(x)
}
// Sinh returns the value of the hyperbolic sine of a number
func Sinh(x float64) float64 {
return math.Sinh(x)
}
// Sqrt returns the value of the square root of a number
func Sqrt(x float64) float64 {
return math.Sqrt(x)
}
// Tan returns the value of the tangent of an angle
func Tan(x float64) float64 {
return math.Tan(x)
}
// Tanh returns the value of the hyperbolic tangent of a number
func Tanh(x float64) float64 {
return math.Tanh(x)
}
// Trunc returns the value of the trunc of a number
func Trunc(x float64) float64 {
return float64(int(x))
}