EHS
Loading...
Searching...
No Matches
Math.h
Go to the documentation of this file.
1#pragma once
2
3#include "ehs/system/CPU.h"
4
5#define EHS_LOW_WORD(x) *((int*)&x) + 1
6
7namespace ehs
8{
9 class EHS_LIB_IO Math
10 {
11 private:
12 static float Sqrt_AVX(const float from);
13
14 static double Sqrt_AVX(const double from);
15
16 static float Sqrt_SSE(const float from);
17
18 static double Sqrt_SSE2(const double from);
19
20 static float Sqrt_VFP4(const float from);
21
22 static double Sqrt_VFP4(const double from);
23
24 public:
25 constexpr static float fltEpsilon = 1e-7f;
26 constexpr static double dblEpsilon = 1e-16;
27
29 static bool AbsCmp(float a, float b);
30
32 static bool AbsCmp(double a, double b);
33
35 static bool RelCmp(float a, float b);
36
38 static bool RelCmp(double a, double b);
39
41 static bool ComCmp(float a, float b);
42
44 static bool ComCmp(double a, double b);
45
46 template<typename T = float>
47 static T Max(const T a, const T b)
48 {
49 return a > b ? a : b;
50 }
51
52 template<typename T = float>
53 static T Min(const T a, const T b)
54 {
55 return a < b ? a : b;
56 }
57
58 template<typename T = float>
59 static T Clamp(const T value, const T min, const T max)
60 {
61 if (value < min)
62 return min;
63 else if (value > max)
64 return max;
65
66 return value;
67 }
68
69 template<typename T = float>
70 static T Abs(const T from)
71 {
72 return from < 0 ? -from : from;
73 }
74
78 template<typename T = float>
79 static constexpr T Pi()
80 {
81 return (T)3.141592653589793238462643383279502884L;
82 }
83
88 template<typename T = float>
89 static T Rads(const T from)
90 {
91 return from * 0.01745329251994329576923690768489;
92 }
93
98 template<typename T = float>
99 static T Degr(const T from)
100 {
101 return from * 57.295779513082320876798154814105;
102 }
103
104 template <typename T = float>
105 static T Exp(const T x)
106 {
107 T sum = 1;
108 T term = 1;
109
110 for (int n = 1; n <= 20; ++n)
111 {
112 term *= x / n;
113 sum += term;
114 }
115
116 return sum;
117 }
118
119 template <typename T = float>
120 static T Ln_Taylor(T x)
121 {
122 T result = 0;
123 T term = (x - 1) / (x + 1);
124 T term_squared = term * term;
125 T denominator = 1;
126 T current_term = term;
127
128 for (int n = 0; n < 100; ++n)
129 {
130 result += current_term / denominator;
131 current_term *= term_squared;
132 denominator += 2;
133 }
134
135 return 2 * result;
136 }
137
138 template <typename T = float>
139 static T Ln(T x)
140 {
141 if (x <= 0)
142 return -1;
143
144 if (x == 1)
145 return 0;
146
147 SSize exp = 0;
148 while (x > 2)
149 {
150 x /= 2;
151 exp++;
152 }
153
154 while (x < 0.5)
155 {
156 x *= 2;
157 exp--;
158 }
159
160 T result = Ln_Taylor<T>(x);
161 result += exp * Ln_Taylor<T>(2);
162
163 return result;
164 }
165
172 template<typename T = float, typename I = float>
173 static T Pow(const T base, const I exponent)
174 {
175 if (base == 0)
176 return (exponent == 0) ? 1 : 0;
177
178 if (exponent == 0)
179 return 1;
180
181 SSize intExp = (SSize)exponent;
182 bool isInteger = exponent == intExp;
183 bool isNeg = base < 0;
184
185 if (isNeg && isInteger)
186 {
187 T result = Exp<T>(exponent * Ln<T>(-base));
188 if ((SSize)exponent % 2)
189 result = -result;
190
191 return result;
192 }
193
194 if (isNeg && !isInteger)
195 {
196 T magnitude = Exp<T>(exponent * Ln<T>(-base));
197 T angle = exponent * Pi<T>();
198 T realPart = magnitude * Cos<T>(angle);
199 T imagPart = magnitude * Sin<T>(angle);
200
201 return realPart;
202 }
203
204 return Exp<T>(exponent * Ln<T>(base));
205 }
206
207 template <typename T = float>
208 static T Log10(const T x)
209 {
210 return Ln<T>(x) / Ln<T>(10);
211 }
212
213 static float Near(const float from);
214
215 static double Near(const double from);
216
217 static float Floor(const float from);
218
219 static double Floor(const double from);
220
221 static float Ceil(const float from);
222
223 static double Ceil(const double from);
224
225 static float Trunc(const float from);
226
227 static double Trunc(const double from);
228
229 static float Mod(const float from, const float divisor);
230
231 static double Mod(const double from, const double divisor);
232
237 template<typename T = float>
238 static T Sqrt(const T from)
239 {
240 T temp = 0;
241 T result = from / 2;
242
243 while (result != temp)
244 {
245 temp = result;
246 result = (from / temp + temp) / 2;
247 }
248
249 return result;
250 }
251
252 static double Sqrt(const double from);
253
254 static float Sqrt(const float from);
255
256 template<typename R = float>
257 static R Sin(const R angle, const R precision = 0.001)
258 {
259 R sum = angle;
260 R term = angle;
261
262 for (UInt_64 i = 1; Abs<R>(term) >= precision; ++i)
263 {
264 term *= -angle * angle / (R)((2 * i + 1) * (2 * i));
265 sum += term;
266 }
267
268 return sum;
269
270 /*
271 R sum = 0;
272
273 for (USize n = 0; n < precision; ++n)
274 sum += Pow<R>(-1, n) / (R)Fact<T>(2 * n + 1) * Pow<R>(angle, 2 * n + 1);
275
276 return sum;
277 */
278 }
279
280 template<typename R = float, typename T = UInt_64>
281 static R ASin(const R yPos, const T precision = 10)
282 {
283 R sum = 0;
284
285 for (T n = 0; n < precision; ++n)
286 sum += (R)Fact<T>(2 * n) / (Pow<R>(4, n) * Pow<R>((R)Fact<T>(n), 2) * (2 * n + 1)) * Pow<R>(yPos, 2 * n + 1);
287
288 return sum;
289 }
290
297 template<typename R = float>
298 static R Cos(const R angle, const R precision = 0.001)
299 {
300 R sum = 1.0;
301 R term = 1.0;
302
303 for (UInt_64 i = 2; Abs<R>(term) >= precision; i += 2)
304 {
305 term *= -angle * angle / (R)(i * (i - 1));
306 sum += term;
307 }
308
309 return sum;
310
311 /*
312 R sum = 0;
313
314 for (T n = 0; n < precision; ++n)
315 sum += Pow<R>(-1, n) / (R)Fact<T>(2 * n) * Pow<R>(angle, 2 * n);
316
317 return sum;
318 */
319 }
320
327 template<typename R = float, typename T = UInt_64>
328 static R ACos(const R xPos, const T precision = 10)
329 {
330 return Pi<R>() / 2 - ASin<R, T>(xPos, precision);
331 }
332
333 template<typename R = float>
334 static R Tan(const R angle, const R precision = 0.001)
335 {
336 /*
337 R sum = 0;
338
339 for (T n = 1; n < precision + 1; ++n)
340 sum += B<R>(2 * n) * Pow<R>(-4, n) * (1 - Pow<R>(4, n)) / (R)Fact<T>(2 * n) * Pow<R>(angle, 2 * n - 1);
341
342 return sum;
343 */
344 return Sin<R>(angle) / Cos<R>(angle);
345 }
346
347 template<typename R = float, typename T = UInt_64>
348 static R ATan(const R x, const T precision = 1)
349 {
350 R sum = 0;
351
352 for (T n = 0; n < precision; ++n)
353 sum += Pow<R>(-1, n) / (2 * n + 1) * Pow<R>(x, 2 * n + 1);
354
355 return sum;
356 }
357
358 template<typename R = float>
359 static R Cot(const R x, const R precision = 0.001)
360 {
361 return 1 / Tan<R>(x, precision);
362 }
363
364 template<typename T = UInt_64>
365 static T Fact(const T n)
366 {
367 if (n <= 1)
368 return 1;
369
370 return n * Fact<T>(n - 1);
371 }
372
373 template<typename R = float, typename T = UInt_64>
374 static R Combination(const T n, const T k)
375 {
376 if (k <= n)
377 return (R)Fact<T>(n) / (R)(Fact<T>(n - k) * Fact<T>(k));
378
379 return 0;
380 }
381
382 template<typename R = float, typename T = UInt_64>
383 static R B(const T n)
384 {
385 R innerSum = 0;
386 R outerSum = 0;
387
388 for (T k = 0; k <= n; ++k)
389 {
390 for (T r = 0; r <= k; ++r)
391 innerSum += Pow<R, T>(-1, r) * Combination<R, T>(k, r) * Pow<R, T>(r, n);
392
393 outerSum += 1 / ((R)k + 1) * innerSum;
394 innerSum = 0;
395 }
396
397 return outerSum;
398 }
399 };
400}
Definition Math.h:10
static T Max(const T a, const T b)
Definition Math.h:47
static constexpr double dblEpsilon
Definition Math.h:26
static T Clamp(const T value, const T min, const T max)
Definition Math.h:59
static R Combination(const T n, const T k)
Definition Math.h:374
static double Floor(const double from)
static R ATan(const R x, const T precision=1)
Definition Math.h:348
static constexpr T Pi()
Definition Math.h:79
static T Min(const T a, const T b)
Definition Math.h:53
static bool AbsCmp(float a, float b)
Absolute tolerance comparison for single precision floats.
Definition Math.cpp:6
static T Pow(const T base, const I exponent)
Definition Math.h:173
static T Degr(const T from)
Definition Math.h:99
static T Rads(const T from)
Definition Math.h:89
static constexpr float fltEpsilon
Definition Math.h:25
static R Sin(const R angle, const R precision=0.001)
Definition Math.h:257
static float Trunc(const float from)
static R Cot(const R x, const R precision=0.001)
Definition Math.h:359
static T Ln_Taylor(T x)
Definition Math.h:120
static double Near(const double from)
static R Cos(const R angle, const R precision=0.001)
Definition Math.h:298
static T Ln(T x)
Definition Math.h:139
static float Ceil(const float from)
static R B(const T n)
Definition Math.h:383
static float Mod(const float from, const float divisor)
Definition Math.cpp:82
static T Fact(const T n)
Definition Math.h:365
static R ACos(const R xPos, const T precision=10)
Definition Math.h:328
static T Log10(const T x)
Definition Math.h:208
static bool RelCmp(float a, float b)
Relative tolerance comparison for single precision floats.
Definition Math.cpp:16
static R Tan(const R angle, const R precision=0.001)
Definition Math.h:334
static float Floor(const float from)
static bool ComCmp(float a, float b)
Combined absolute and relative tolerance comparison for single precision floats.
Definition Math.cpp:26
static T Abs(const T from)
Definition Math.h:70
static float Near(const float from)
static T Exp(const T x)
Definition Math.h:105
static T Sqrt(const T from)
Definition Math.h:238
static double Ceil(const double from)
static double Trunc(const double from)
static R ASin(const R yPos, const T precision=10)
Definition Math.h:281
Definition Anchor.h:6