FifeGUI 0.3.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// Standard library includes
9#include <cassert>
10#include <cmath>
11#include <limits>
12#include <numbers>
13
14// Platform config include
15#include "fifechan/platform.hpp"
16
17namespace fcn
18{
19 static float const FLT_STD_EPSILON = std::numeric_limits<float>::epsilon();
20 static float const FLT_STD_MAX = std::numeric_limits<float>::max();
21 static float const FLT_ZERO_TOLERANCE = 1e-06F;
22 static float const FLT_PI = 4.0F * std::atan(1.0F);
23 static float const FLT_TWO_PI = 2.0F * FLT_PI;
24 static float const FLT_HALF_PI = 0.5F * FLT_PI;
25 static float const FLT_INVERSE_PI = 1.0F / FLT_PI;
26 static float const FLT_INVERSE_TWO_PI = 1.0F / FLT_TWO_PI;
27 static float const FLT_DEG_TO_RAD = FLT_PI / 180.0F;
28 static float const FLT_RAD_TO_DEG = 180.0F / FLT_PI;
29 static float const FLT_LOG_2 = std::numbers::ln2_v<float>;
30 static float const FLT_LOG_10 = std::numbers::ln10_v<float>;
31 static float const FLT_INV_LOG_2 = 1.0F / std::numbers::ln2_v<float>;
32 static float const FLT_INV_LOG_10 = 1.0F / std::numbers::ln10_v<float>;
33
34 static double const DBL_STD_EPSILON = std::numeric_limits<double>::epsilon();
35 static double const DBL_STD_MAX = std::numeric_limits<double>::max();
36 static double const DBL_ZERO_TOLERANCE = 1e-08;
37 static double const DBL_PI = 4.0 * std::atan(1.0);
38 static double const DBL_TWO_PI = 2.0 * DBL_PI;
39 static double const DBL_HALF_PI = 0.5 * DBL_PI;
40 static double const DBL_INVERSE_PI = 1.0 / DBL_PI;
41 static double const DBL_INVERSE_TWO_PI = 1.0 / DBL_TWO_PI;
42 static double const DBL_DEG_TO_RAD = DBL_PI / 180.0;
43 static double const DBL_RAD_TO_DEG = 180.0F / DBL_PI;
44 static double const DBL_LOG_2 = std::numbers::ln2;
45 static double const DBL_LOG_10 = std::numbers::ln10;
46 static double const DBL_INV_LOG_2 = 1.0 / std::numbers::ln2;
47 static double const DBL_INV_LOG_10 = 1.0 / std::numbers::ln10;
48
57 template <class numT>
59 {
60 };
61
68 template <>
69 struct float_traits<float>
70 {
71 using float_type = float;
72
75 {
76 return FLT_STD_EPSILON;
77 }
78
80 {
81 return FLT_ZERO_TOLERANCE;
82 }
83
84 static float_type max()
85 {
86 return FLT_STD_MAX;
87 }
88
89 static float_type pi()
90 {
91 return FLT_PI;
92 }
93
95 {
96 return FLT_TWO_PI;
97 }
98
100 {
101 return FLT_HALF_PI;
102 }
103
105 {
106 return FLT_INVERSE_PI;
107 }
108
110 {
111 return FLT_INVERSE_TWO_PI;
112 }
113
115 {
116 return FLT_DEG_TO_RAD;
117 }
118
120 {
121 return FLT_RAD_TO_DEG;
122 }
123
125 {
126 return FLT_LOG_2;
127 }
128
130 {
131 return FLT_LOG_10;
132 }
133
135 {
136 return FLT_INV_LOG_2;
137 }
138
140 {
141 return FLT_INV_LOG_10;
142 }
143 };
144
151 template <>
152 struct float_traits<double>
153 {
154 using float_type = double;
155
158 {
159 return DBL_STD_EPSILON;
160 }
161
163 {
164 return DBL_ZERO_TOLERANCE;
165 }
166
168 {
169 return DBL_STD_MAX;
170 }
171
172 static float_type pi()
173 {
174 return DBL_PI;
175 }
176
178 {
179 return DBL_TWO_PI;
180 }
181
183 {
184 return DBL_HALF_PI;
185 }
186
188 {
189 return DBL_INVERSE_PI;
190 }
191
193 {
194 return DBL_INVERSE_TWO_PI;
195 }
196
198 {
199 return DBL_DEG_TO_RAD;
200 }
201
203 {
204 return DBL_RAD_TO_DEG;
205 }
206
208 {
209 return DBL_LOG_2;
210 }
211
213 {
214 return DBL_LOG_10;
215 }
216
218 {
219 return DBL_INV_LOG_2;
220 }
221
223 {
224 return DBL_INV_LOG_10;
225 }
226 };
227
236 template <typename T>
237 class Math
238 {
239 public:
243 using num_type = T;
244
247
250 {
251 return traits_type::epsilon();
252 }
253
256 {
257 return traits_type::zeroTolerance();
258 }
259
261 static num_type max()
262 {
263 return traits_type::max();
264 }
265
267 static num_type pi()
268 {
269 return traits_type::pi();
270 }
271
274 {
275 return traits_type::twoPi();
276 }
277
280 {
281 return traits_type::halfPi();
282 }
283
286 {
287 return traits_type::inversePi();
288 }
289
292 {
293 return traits_type::inverseTwoPi();
294 }
295
298 {
299 return traits_type::degToRad();
300 }
301
304 {
305 return traits_type::radToDeg();
306 }
307
309 static num_type log2()
310 {
311 return traits_type::log2();
312 }
313
316 {
317 return traits_type::log10();
318 }
319
322 {
323 return traits_type::invLog2();
324 }
325
328 {
329 return traits_type::invLog10();
330 }
331
333 static T ACos(T _val);
334
336 static T ASin(T _val);
337
339 static T ATan(T _val);
340
342 static T ATan2(T _x, T _y);
343
345 static T Ceil(T _val);
346
348 static T Cos(T _val);
349
351 static T Exp(T _val);
352
354 static T FAbs(T _val);
355
357 static T Floor(T _val);
358
360 static T FMod(T _x, T _y);
361
363 static T InvSqrt(T _val);
364
366 static T Log(T _val);
367
369 static T Log2(T _val);
370
372 static T Log10(T _val);
373
375 static T Pow(T _base, T _exponent);
376
378 static T Sin(T _val);
379
381 static T Sqr(T _val);
382
384 static T Sqrt(T _val);
385
387 static T Tan(T _val);
388
390 static bool Equal(T _val1, T _val2);
391 };
392
397
402
403 template <typename T>
404 inline T Math<T>::ACos(T _val)
405 {
406 if (-static_cast<T>(1) < _val) {
407 if (_val < static_cast<T>(1)) {
408 return static_cast<T>(std::acos(_val));
409 }
410
411 return static_cast<T>(0);
412 }
413
414 return pi();
415 }
416
417 template <class T>
418 inline T Math<T>::ASin(T _val)
419 {
420 if (-static_cast<T>(1) < _val) {
421 if (_val < static_cast<T>(1)) {
422 return static_cast<T>(std::asin(_val));
423 }
424
425 return halfPi();
426 }
427
428 return -halfPi();
429 }
430
431 template <class T>
432 inline T Math<T>::ATan(T _val)
433 {
434 return static_cast<T>(std::atan(_val));
435 }
436
437 template <class T>
438 inline T Math<T>::ATan2(T _x, T _y)
439 {
440 return static_cast<T>(std::atan2(_x, _y));
441 }
442
443 template <class T>
444 inline T Math<T>::Ceil(T _val)
445 {
446 return static_cast<T>(std::ceil(_val));
447 }
448
449 template <class T>
450 inline T Math<T>::Cos(T _val)
451 {
452 return static_cast<T>(std::cos(_val));
453 }
454
455 template <class T>
456 inline T Math<T>::Exp(T _val)
457 {
458 return static_cast<T>(std::exp(_val));
459 }
460
461 template <class T>
462 inline T Math<T>::FAbs(T _val)
463 {
464 return static_cast<T>(std::fabs(_val));
465 }
466
467 template <class T>
468 inline T Math<T>::Floor(T _val)
469 {
470 return static_cast<T>(std::floor(_val));
471 }
472
473 template <class T>
474 inline T Math<T>::FMod(T _x, T _y)
475 {
476 return static_cast<T>(std::fmod(_x, _y));
477 }
478
479 template <class T>
480 inline T Math<T>::InvSqrt(T _val)
481 {
482 return static_cast<T>(1 / std::sqrt(_val));
483 }
484
485 template <class T>
486 inline T Math<T>::Log(T _val)
487 {
488 return static_cast<T>(std::log(_val));
489 }
490
491 template <class T>
492 inline T Math<T>::Log2(T _val)
493 {
494 return invLog2() * static_cast<T>(std::log(_val));
495 }
496 template <class T>
497 inline T Math<T>::Log10(T _val)
498 {
499 return invLog10() * static_cast<T>(std::log(_val));
500 }
501
502 template <class T>
503 inline T Math<T>::Pow(T _base, T _exponent)
504 {
505 return static_cast<T>(std::pow(_base, _exponent));
506 }
507
508 template <class T>
509 inline T Math<T>::Sin(T _val)
510 {
511 return static_cast<T>(std::sin(_val));
512 }
513
514 template <class T>
515 inline T Math<T>::Sqr(T _val)
516 {
517 return _val * _val;
518 }
519
520 template <class T>
521 inline T Math<T>::Sqrt(T _val)
522 {
523 return static_cast<T>(std::sqrt(_val));
524 }
525
526 template <class T>
527 inline T Math<T>::Tan(T _val)
528 {
529 return static_cast<T>(std::tan(_val));
530 }
531
532 template <class T>
533 inline bool Math<T>::Equal(T _val1, T _val2)
534 {
535 return std::fabs(_val1 - _val2) < epsilon();
536 }
537
544 inline unsigned nextPow2(unsigned x)
545 {
546 --x;
547 x |= x >> 1;
548 x |= x >> 2;
549 x |= x >> 4;
550 x |= x >> 8;
551 x |= x >> 16;
552 return ++x;
553 }
554} // namespace fcn
555
556#endif // INCLUDE_FIFECHAN_MATH_HPP_
Template utility providing math functions and constants for numeric type T.
Definition math.hpp:238
static num_type log10()
Natural log base 10.
Definition math.hpp:315
static num_type zeroTolerance()
Recommended zero tolerance for comparisons.
Definition math.hpp:255
static T Sqrt(T _val)
Square root.
Definition math.hpp:521
static T ATan(T _val)
Arc tangent.
Definition math.hpp:432
static num_type invLog10()
1/ln(10) helper.
Definition math.hpp:327
float_traits< num_type > traits_type
Trait alias providing numeric constants and helpers for num_type.
Definition math.hpp:246
static T Tan(T _val)
Tangent of value.
Definition math.hpp:527
static bool Equal(T _val1, T _val2)
Approximate equality check using epsilon.
Definition math.hpp:533
static num_type inversePi()
1/pi constant.
Definition math.hpp:285
static T Pow(T _base, T _exponent)
Power function.
Definition math.hpp:503
static num_type radToDeg()
Radians-to-degrees factor.
Definition math.hpp:303
static T FMod(T _x, T _y)
Floating-point modulus.
Definition math.hpp:474
static num_type log2()
Natural log base 2.
Definition math.hpp:309
static num_type twoPi()
Two times pi.
Definition math.hpp:273
static T ACos(T _val)
Arc cosine (clamped).
Definition math.hpp:404
static num_type epsilon()
Returns machine epsilon for the numeric type.
Definition math.hpp:249
static T Log10(T _val)
Log base 10.
Definition math.hpp:497
static T Ceil(T _val)
Ceiling of value.
Definition math.hpp:444
static T Cos(T _val)
Cosine of value.
Definition math.hpp:450
static num_type pi()
Pi constant.
Definition math.hpp:267
static T Sin(T _val)
Sine of value.
Definition math.hpp:509
static T Floor(T _val)
Floor of value.
Definition math.hpp:468
static T Log2(T _val)
Log base 2.
Definition math.hpp:492
static T Exp(T _val)
Exponential e^x.
Definition math.hpp:456
static T Sqr(T _val)
Square of value.
Definition math.hpp:515
static num_type invLog2()
1/ln(2) helper.
Definition math.hpp:321
T num_type
Type definitions.
Definition math.hpp:243
static num_type inverseTwoPi()
1/(2*pi) constant.
Definition math.hpp:291
static T ASin(T _val)
Arc sine (clamped).
Definition math.hpp:418
static num_type max()
Maximum finite value for the numeric type.
Definition math.hpp:261
static T FAbs(T _val)
Absolute value (floating).
Definition math.hpp:462
static num_type degToRad()
Degrees-to-radians factor.
Definition math.hpp:297
static T InvSqrt(T _val)
Inverse square root (1/sqrt(x)).
Definition math.hpp:480
static T ATan2(T _x, T _y)
Two-argument arc tangent.
Definition math.hpp:438
static num_type halfPi()
Half of pi.
Definition math.hpp:279
static T Log(T _val)
Natural logarithm.
Definition math.hpp:486
Used replacement tokens by configure_file():
Math< float > Mathf
Math utilities for floats.
Definition math.hpp:396
unsigned nextPow2(unsigned x)
Returns the next higher power of 2 based on the passed argument.
Definition math.hpp:544
Math< double > Mathd
Math utilities for doubles.
Definition math.hpp:401
static float_type invLog2()
1 / ln(2) for double conversions.
Definition math.hpp:217
static float_type degToRad()
Degrees to radians conversion factor.
Definition math.hpp:197
static float_type log2()
Natural log base 2 for doubles.
Definition math.hpp:207
static float_type inversePi()
1/pi constant.
Definition math.hpp:187
static float_type radToDeg()
Radians to degrees conversion factor.
Definition math.hpp:202
static float_type twoPi()
Two times pi.
Definition math.hpp:177
static float_type pi()
Pi constant for double.
Definition math.hpp:172
static float_type zeroTolerance()
Recommended zero tolerance for double comparisons.
Definition math.hpp:162
static float_type inverseTwoPi()
1/(2*pi) constant.
Definition math.hpp:192
static float_type halfPi()
Half of pi.
Definition math.hpp:182
double float_type
Floating-point typedef for this specialization.
Definition math.hpp:154
static float_type epsilon()
Machine epsilon for double.
Definition math.hpp:157
static float_type invLog10()
1 / ln(10) for double conversions.
Definition math.hpp:222
static float_type log10()
Natural log base 10 for doubles.
Definition math.hpp:212
static float_type max()
Maximum finite double value.
Definition math.hpp:167
static float_type inverseTwoPi()
1/(2*pi) constant.
Definition math.hpp:109
static float_type log2()
Natural log base 2 for floats.
Definition math.hpp:124
static float_type invLog10()
1 / ln(10) for float conversions.
Definition math.hpp:139
static float_type invLog2()
1 / ln(2) for float conversions.
Definition math.hpp:134
static float_type radToDeg()
Radians to degrees conversion factor.
Definition math.hpp:119
static float_type degToRad()
Degrees to radians conversion factor.
Definition math.hpp:114
static float_type max()
Maximum finite float value.
Definition math.hpp:84
static float_type inversePi()
1/pi constant.
Definition math.hpp:104
static float_type zeroTolerance()
Recommended zero tolerance for float comparisons.
Definition math.hpp:79
static float_type twoPi()
Two times pi.
Definition math.hpp:94
static float_type halfPi()
Half of pi.
Definition math.hpp:99
static float_type pi()
Pi constant for floats.
Definition math.hpp:89
float float_type
Floating-point typedef for this specialization.
Definition math.hpp:71
static float_type log10()
Natural log base 10 for floats.
Definition math.hpp:129
static float_type epsilon()
Machine epsilon for float.
Definition math.hpp:74
Generic template for floating-point type traits.
Definition math.hpp:59