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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*************************************************************************/
/* math_funcs.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "math_funcs.h"
#include "core/os/os.h"
#include <math.h>
#include "float.h"
uint32_t Math::default_seed=1;
#define PHI 0x9e3779b9
#if 0
static uint32_t Q[4096];
#endif
uint32_t Math::rand_from_seed(uint32_t *seed) {
// Xorshift31 PRNG
if ( *seed == 0 ) *seed = Math::RANDOM_MAX;
(*seed) ^= (*seed) << 13;
(*seed) ^= (*seed) >> 17;
(*seed) ^= (*seed) << 5;
return (*seed) & Math::RANDOM_MAX;
}
void Math::seed(uint32_t x) {
default_seed=x;
}
void Math::randomize() {
OS::Time time = OS::get_singleton()->get_time();
seed(OS::get_singleton()->get_ticks_usec()*(time.hour+1)*(time.min+1)*(time.sec+1)*rand()); /* *OS::get_singleton()->get_time().sec); // windows doesn't have get_time(), returns always 0 */
}
uint32_t Math::rand() {
return rand_from_seed(&default_seed);
}
double Math::randf() {
return (double)rand() / (double)Math::RANDOM_MAX;
}
double Math::sin(double p_x) {
return ::sin(p_x);
}
double Math::cos(double p_x) {
return ::cos(p_x);
}
double Math::tan(double p_x) {
return ::tan(p_x);
}
double Math::sinh(double p_x) {
return ::sinh(p_x);
}
double Math::cosh(double p_x) {
return ::cosh(p_x);
}
double Math::tanh(double p_x) {
return ::tanh(p_x);
}
double Math::deg2rad(double p_y) {
return p_y*Math_PI/180.0;
}
double Math::rad2deg(double p_y) {
return p_y*180.0/Math_PI;
}
double Math::round(double p_val) {
if (p_val>=0) {
return ::floor(p_val+0.5);
} else {
p_val=-p_val;
return -::floor(p_val+0.5);
}
}
double Math::asin(double p_x) {
return ::asin(p_x);
}
double Math::acos(double p_x) {
return ::acos(p_x);
}
double Math::atan(double p_x) {
return ::atan(p_x);
}
double Math::dectime(double p_value,double p_amount, double p_step) {
float sgn = p_value < 0 ? -1.0 : 1.0;
float val = absf(p_value);
val-=p_amount*p_step;
if (val<0.0)
val=0.0;
return val*sgn;
}
double Math::atan2(double p_y, double p_x) {
return ::atan2(p_y,p_x);
}
double Math::sqrt(double p_x) {
return ::sqrt(p_x);
}
double Math::fmod(double p_x,double p_y) {
return ::fmod(p_x,p_y);
}
double Math::fposmod(double p_x,double p_y) {
if (p_x>=0) {
return Math::fmod(p_x,p_y);
} else {
return p_y-Math::fmod(-p_x,p_y);
}
}
double Math::floor(double p_x) {
return ::floor(p_x);
}
double Math::ceil(double p_x) {
return ::ceil(p_x);
}
int Math::step_decimals(double p_step) {
static const int maxn=9;
static const double sd[maxn]={
0.9999, // somehow compensate for floating point error
0.09999,
0.009999,
0.0009999,
0.00009999,
0.000009999,
0.0000009999,
0.00000009999,
0.000000009999
};
double as=absf(p_step);
for(int i=0;i<maxn;i++) {
if (as>=sd[i]) {
return i;
}
}
return maxn;
}
double Math::ease(double p_x, double p_c) {
if (p_x<0)
p_x=0;
else if (p_x>1.0)
p_x=1.0;
if (p_c>0) {
if (p_c<1.0) {
return 1.0-Math::pow(1.0-p_x,1.0/p_c);
} else {
return Math::pow(p_x,p_c);
}
} else if (p_c<0) {
//inout ease
if (p_x<0.5) {
return Math::pow(p_x*2.0,-p_c)*0.5;
} else {
return (1.0-Math::pow(1.0-(p_x-0.5)*2.0,-p_c))*0.5+0.5;
}
} else
return 0; // no ease (raw)
}
double Math::stepify(double p_value,double p_step) {
if (p_step!=0) {
p_value=floor( p_value / p_step + 0.5 ) * p_step;
}
return p_value;
}
bool Math::is_nan(double p_val) {
return (p_val!=p_val);
}
bool Math::is_inf(double p_val) {
#ifdef _MSC_VER
return !_finite(p_val);
#else
return isinf(p_val);
#endif
}
uint32_t Math::larger_prime(uint32_t p_val) {
static const uint32_t primes[] = {
5,
13,
23,
47,
97,
193,
389,
769,
1543,
3079,
6151,
12289,
24593,
49157,
98317,
196613,
393241,
786433,
1572869,
3145739,
6291469,
12582917,
25165843,
50331653,
100663319,
201326611,
402653189,
805306457,
1610612741,
0,
};
int idx=0;
while (true) {
ERR_FAIL_COND_V(primes[idx]==0,0);
if (primes[idx]>p_val)
return primes[idx];
idx++;
}
return 0;
}
double Math::random(double from, double to) {
unsigned int r = Math::rand();
double ret = (double)r/(double)RANDOM_MAX;
return (ret)*(to-from) + from;
}
double Math::pow(double x, double y) {
return ::pow(x,y);
}
double Math::log(double x) {
return ::log(x);
}
double Math::exp(double x) {
return ::exp(x);
}
|