FifeGUI 0.2.0
A C++ GUI library designed for games.
math.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5#ifndef INCLUDE_FIFECHAN_MATH_HPP_
6#define INCLUDE_FIFECHAN_MATH_HPP_
7
8#include <cassert>
9#include <cmath>
10#include <limits>
11#include <numbers>
12
13namespace fcn
14{
15 static float const FLT_STD_EPSILON = std::numeric_limits<float>::epsilon();
16 static float const FLT_STD_MAX = std::numeric_limits<float>::max();
17 static float const FLT_ZERO_TOLERANCE = 1e-06F;
18 static float const FLT_PI = 4.0F * std::atan(1.0F);
19 static float const FLT_TWO_PI = 2.0F * FLT_PI;
20 static float const FLT_HALF_PI = 0.5F * FLT_PI;
21 static float const FLT_INVERSE_PI = 1.0F / FLT_PI;
22 static float const FLT_INVERSE_TWO_PI = 1.0F / FLT_TWO_PI;
23 static float const FLT_DEG_TO_RAD = FLT_PI / 180.0F;
24 static float const FLT_RAD_TO_DEG = 180.0F / FLT_PI;
25 static float const FLT_LOG_2 = std::numbers::ln2_v<float>;
26 static float const FLT_LOG_10 = std::numbers::ln10_v<float>;
27 static float const FLT_INV_LOG_2 = 1.0F / std::numbers::ln2_v<float>;
28 static float const FLT_INV_LOG_10 = 1.0F / std::numbers::ln10_v<float>;
29
30 static double const DBL_STD_EPSILON = std::numeric_limits<double>::epsilon();
31 static double const DBL_STD_MAX = std::numeric_limits<double>::max();
32 static double const DBL_ZERO_TOLERANCE = 1e-08;
33 static double const DBL_PI = 4.0 * std::atan(1.0);
34 static double const DBL_TWO_PI = 2.0 * DBL_PI;
35 static double const DBL_HALF_PI = 0.5 * DBL_PI;
36 static double const DBL_INVERSE_PI = 1.0 / DBL_PI;
37 static double const DBL_INVERSE_TWO_PI = 1.0 / DBL_TWO_PI;
38 static double const DBL_DEG_TO_RAD = DBL_PI / 180.0;
39 static double const DBL_RAD_TO_DEG = 180.0F / DBL_PI;
40 static double const DBL_LOG_2 = std::numbers::ln2;
41 static double const DBL_LOG_10 = std::numbers::ln10;
42 static double const DBL_INV_LOG_2 = 1.0 / std::numbers::ln2;
43 static double const DBL_INV_LOG_10 = 1.0 / std::numbers::ln10;
44
53 template <class numT>
55 {
56 };
57
64 template <>
65 struct float_traits<float>
66 {
67 using float_type = float;
68
71 {
72 return FLT_STD_EPSILON;
73 }
74
76 {
77 return FLT_ZERO_TOLERANCE;
78 }
79
80 static float_type max()
81 {
82 return FLT_STD_MAX;
83 }
84
85 static float_type pi()
86 {
87 return FLT_PI;
88 }
89
91 {
92 return FLT_TWO_PI;
93 }
94
96 {
97 return FLT_HALF_PI;
98 }
99
101 {
102 return FLT_INVERSE_PI;
103 }
104
106 {
107 return FLT_INVERSE_TWO_PI;
108 }
109
111 {
112 return FLT_DEG_TO_RAD;
113 }
114
116 {
117 return FLT_RAD_TO_DEG;
118 }
119
121 {
122 return FLT_LOG_2;
123 }
124
126 {
127 return FLT_LOG_10;
128 }
129
131 {
132 return FLT_INV_LOG_2;
133 }
134
136 {
137 return FLT_INV_LOG_10;
138 }
139 };
140
147 template <>
148 struct float_traits<double>
149 {
150 using float_type = double;
151
154 {
155 return DBL_STD_EPSILON;
156 }
157
159 {
160 return DBL_ZERO_TOLERANCE;
161 }
162
164 {
165 return DBL_STD_MAX;
166 }
167
168 static float_type pi()
169 {
170 return DBL_PI;
171 }
172
174 {
175 return DBL_TWO_PI;
176 }
177
179 {
180 return DBL_HALF_PI;
181 }
182
184 {
185 return DBL_INVERSE_PI;
186 }
187
189 {
190 return DBL_INVERSE_TWO_PI;
191 }
192
194 {
195 return DBL_DEG_TO_RAD;
196 }
197
199 {
200 return DBL_RAD_TO_DEG;
201 }
202
204 {
205 return DBL_LOG_2;
206 }
207
209 {
210 return DBL_LOG_10;
211 }
212
214 {
215 return DBL_INV_LOG_2;
216 }
217
219 {
220 return DBL_INV_LOG_10;
221 }
222 };
223
232 template <typename T>
233 class Math
234 {
235 public:
239 using num_type = T;
240
243
246 {
247 return traits_type::epsilon();
248 }
249
252 {
253 return traits_type::zeroTolerance();
254 }
255
257 static num_type max()
258 {
259 return traits_type::max();
260 }
261
263 static num_type pi()
264 {
265 return traits_type::pi();
266 }
267
270 {
271 return traits_type::twoPi();
272 }
273
276 {
277 return traits_type::halfPi();
278 }
279
282 {
283 return traits_type::inversePi();
284 }
285
288 {
289 return traits_type::inverseTwoPi();
290 }
291
294 {
295 return traits_type::degToRad();
296 }
297
300 {
301 return traits_type::radToDeg();
302 }
303
305 static num_type log2()
306 {
307 return traits_type::log2();
308 }
309
312 {
313 return traits_type::log10();
314 }
315
318 {
319 return traits_type::invLog2();
320 }
321
324 {
325 return traits_type::invLog10();
326 }
327
329 static T ACos(T _val);
330
332 static T ASin(T _val);
333
335 static T ATan(T _val);
336
338 static T ATan2(T _x, T _y);
339
341 static T Ceil(T _val);
342
344 static T Cos(T _val);
345
347 static T Exp(T _val);
348
350 static T FAbs(T _val);
351
353 static T Floor(T _val);
354
356 static T FMod(T _x, T _y);
357
359 static T InvSqrt(T _val);
360
362 static T Log(T _val);
363
365 static T Log2(T _val);
366
368 static T Log10(T _val);
369
371 static T Pow(T _base, T _exponent);
372
374 static T Sin(T _val);
375
377 static T Sqr(T _val);
378
380 static T Sqrt(T _val);
381
383 static T Tan(T _val);
384
386 static bool Equal(T _val1, T _val2);
387 };
388
389 using Mathf = Math<float>;
390 using Mathd = Math<double>;
391
392 template <typename T>
393 inline T Math<T>::ACos(T _val)
394 {
395 if (-static_cast<T>(1) < _val) {
396 if (_val < static_cast<T>(1)) {
397 return static_cast<T>(std::acos(_val));
398 }
399
400 return static_cast<T>(0);
401 }
402
403 return pi();
404 }
405
406 template <class T>
407 inline T Math<T>::ASin(T _val)
408 {
409 if (-static_cast<T>(1) < _val) {
410 if (_val < static_cast<T>(1)) {
411 return static_cast<T>(std::asin(_val));
412 }
413
414 return halfPi();
415 }
416
417 return -halfPi();
418 }
419
420 template <class T>
421 inline T Math<T>::ATan(T _val)
422 {
423 return static_cast<T>(std::atan(_val));
424 }
425
426 template <class T>
427 inline T Math<T>::ATan2(T _x, T _y)
428 {
429 return static_cast<T>(std::atan2(_x, _y));
430 }
431
432 template <class T>
433 inline T Math<T>::Ceil(T _val)
434 {
435 return static_cast<T>(std::ceil(_val));
436 }
437
438 template <class T>
439 inline T Math<T>::Cos(T _val)
440 {
441 return static_cast<T>(std::cos(_val));
442 }
443
444 template <class T>
445 inline T Math<T>::Exp(T _val)
446 {
447 return static_cast<T>(std::exp(_val));
448 }
449
450 template <class T>
451 inline T Math<T>::FAbs(T _val)
452 {
453 return static_cast<T>(std::fabs(_val));
454 }
455
456 template <class T>
457 inline T Math<T>::Floor(T _val)
458 {
459 return static_cast<T>(std::floor(_val));
460 }
461
462 template <class T>
463 inline T Math<T>::FMod(T _x, T _y)
464 {
465 return static_cast<T>(std::fmod(_x, _y));
466 }
467
468 template <class T>
469 inline T Math<T>::InvSqrt(T _val)
470 {
471 return static_cast<T>(1 / std::sqrt(_val));
472 }
473
474 template <class T>
475 inline T Math<T>::Log(T _val)
476 {
477 return static_cast<T>(std::log(_val));
478 }
479
480 template <class T>
481 inline T Math<T>::Log2(T _val)
482 {
483 return invLog2() * static_cast<T>(std::log(_val));
484 }
485 template <class T>
486 inline T Math<T>::Log10(T _val)
487 {
488
489 return invLog10() * static_cast<T>(std::log(_val));
490 }
491
492 template <class T>
493 inline T Math<T>::Pow(T _base, T _exponent)
494 {
495 return static_cast<T>(std::pow(_base, _exponent));
496 }
497
498 template <class T>
499 inline T Math<T>::Sin(T _val)
500 {
501 return static_cast<T>(std::sin(_val));
502 }
503
504 template <class T>
505 inline T Math<T>::Sqr(T _val)
506 {
507 return _val * _val;
508 }
509
510 template <class T>
511 inline T Math<T>::Sqrt(T _val)
512 {
513 return static_cast<T>(std::sqrt(_val));
514 }
515
516 template <class T>
517 inline T Math<T>::Tan(T _val)
518 {
519 return static_cast<T>(std::tan(_val));
520 }
521
522 template <class T>
523 inline bool Math<T>::Equal(T _val1, T _val2)
524 {
525 return std::fabs(_val1 - _val2) < epsilon();
526 }
527
531 inline unsigned nextPow2(unsigned x)
532 {
533 --x;
534 x |= x >> 1;
535 x |= x >> 2;
536 x |= x >> 4;
537 x |= x >> 8;
538 x |= x >> 16;
539 return ++x;
540 }
541} // namespace fcn
542
543#endif // INCLUDE_FIFECHAN_MATH_HPP_
Template utility providing math functions and constants for numeric type T.
Definition math.hpp:234
static num_type log10()
Natural log base 10.
Definition math.hpp:311
static num_type zeroTolerance()
Recommended zero tolerance for comparisons.
Definition math.hpp:251
static T Sqrt(T _val)
Square root.
Definition math.hpp:511
static T ATan(T _val)
Arc tangent.
Definition math.hpp:421
static num_type invLog10()
1/ln(10) helper.
Definition math.hpp:323
float_traits< num_type > traits_type
Trait alias providing numeric constants and helpers for num_type.
Definition math.hpp:242
static T Tan(T _val)
Tangent of value.
Definition math.hpp:517
static bool Equal(T _val1, T _val2)
Approximate equality check using epsilon.
Definition math.hpp:523
static num_type inversePi()
1/pi constant.
Definition math.hpp:281
static T Pow(T _base, T _exponent)
Power function.
Definition math.hpp:493
static num_type radToDeg()
Radians-to-degrees factor.
Definition math.hpp:299
static T FMod(T _x, T _y)
Floating-point modulus.
Definition math.hpp:463
static num_type log2()
Natural log base 2.
Definition math.hpp:305
static num_type twoPi()
Two times pi.
Definition math.hpp:269
static T ACos(T _val)
Arc cosine (clamped).
Definition math.hpp:393
static num_type epsilon()
Returns machine epsilon for the numeric type.
Definition math.hpp:245
static T Log10(T _val)
Log base 10.
Definition math.hpp:486
static T Ceil(T _val)
Ceiling of value.
Definition math.hpp:433
static T Cos(T _val)
Cosine of value.
Definition math.hpp:439
static num_type pi()
Pi constant.
Definition math.hpp:263
static T Sin(T _val)
Sine of value.
Definition math.hpp:499
static T Floor(T _val)
Floor of value.
Definition math.hpp:457
static T Log2(T _val)
Log base 2.
Definition math.hpp:481
static T Exp(T _val)
Exponential e^x.
Definition math.hpp:445
static T Sqr(T _val)
Square of value.
Definition math.hpp:505
static num_type invLog2()
1/ln(2) helper.
Definition math.hpp:317
T num_type
Type definitions.
Definition math.hpp:239
static num_type inverseTwoPi()
1/(2*pi) constant.
Definition math.hpp:287
static T ASin(T _val)
Arc sine (clamped).
Definition math.hpp:407
static num_type max()
Maximum finite value for the numeric type.
Definition math.hpp:257
static T FAbs(T _val)
Absolute value (floating).
Definition math.hpp:451
static num_type degToRad()
Degrees-to-radians factor.
Definition math.hpp:293
static T InvSqrt(T _val)
Inverse square root (1/sqrt(x)).
Definition math.hpp:469
static T ATan2(T _x, T _y)
Two-argument arc tangent.
Definition math.hpp:427
static num_type halfPi()
Half of pi.
Definition math.hpp:275
static T Log(T _val)
Natural logarithm.
Definition math.hpp:475
static float_type invLog2()
1 / ln(2) for double conversions.
Definition math.hpp:213
static float_type degToRad()
Degrees to radians conversion factor.
Definition math.hpp:193
static float_type log2()
Natural log base 2 for doubles.
Definition math.hpp:203
static float_type inversePi()
1/pi constant.
Definition math.hpp:183
static float_type radToDeg()
Radians to degrees conversion factor.
Definition math.hpp:198
static float_type twoPi()
Two times pi.
Definition math.hpp:173
static float_type pi()
Pi constant for double.
Definition math.hpp:168
static float_type zeroTolerance()
Recommended zero tolerance for double comparisons.
Definition math.hpp:158
static float_type inverseTwoPi()
1/(2*pi) constant.
Definition math.hpp:188
static float_type halfPi()
Half of pi.
Definition math.hpp:178
double float_type
Floating-point typedef for this specialization.
Definition math.hpp:150
static float_type epsilon()
Machine epsilon for double.
Definition math.hpp:153
static float_type invLog10()
1 / ln(10) for double conversions.
Definition math.hpp:218
static float_type log10()
Natural log base 10 for doubles.
Definition math.hpp:208
static float_type max()
Maximum finite double value.
Definition math.hpp:163
static float_type inverseTwoPi()
1/(2*pi) constant.
Definition math.hpp:105
static float_type log2()
Natural log base 2 for floats.
Definition math.hpp:120
static float_type invLog10()
1 / ln(10) for float conversions.
Definition math.hpp:135
static float_type invLog2()
1 / ln(2) for float conversions.
Definition math.hpp:130
static float_type radToDeg()
Radians to degrees conversion factor.
Definition math.hpp:115
static float_type degToRad()
Degrees to radians conversion factor.
Definition math.hpp:110
static float_type max()
Maximum finite float value.
Definition math.hpp:80
static float_type inversePi()
1/pi constant.
Definition math.hpp:100
static float_type zeroTolerance()
Recommended zero tolerance for float comparisons.
Definition math.hpp:75
static float_type twoPi()
Two times pi.
Definition math.hpp:90
static float_type halfPi()
Half of pi.
Definition math.hpp:95
static float_type pi()
Pi constant for floats.
Definition math.hpp:85
float float_type
Floating-point typedef for this specialization.
Definition math.hpp:67
static float_type log10()
Natural log base 10 for floats.
Definition math.hpp:125
static float_type epsilon()
Machine epsilon for float.
Definition math.hpp:70
Generic template for floating-point type traits.
Definition math.hpp:55