FastLED 3.9.15
Loading...
Searching...
No Matches
math_macros.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/has_include.h"
4
5
6#if FL_HAS_INCLUDE(<math.h>)
7#include <math.h> // for early definitions of M_PI and other math macros.
8#endif // FL_HAS_INCLUDE(<math.h>)
9
10
11#include "fl/compiler_control.h"
12#include "fl/type_traits.h"
13
14namespace fl {
15
16// Fun fact, we can't define any function by the name of min,max,abs because
17// on some platforms these are macros. Therefore we can only use fl_min, fl_max, fl_abs.
18// This is needed for math macro ABS to work optimally.
19template <typename T> inline T fl_abs(T value) {
20 return value < 0 ? -value : value;
21}
22
24FL_DISABLE_WARNING(sign-compare)
29
30
31// Template functions for MIN and MAX to avoid statement repetition
32// Returns the most promotable type between the two arguments
33template <typename T, typename U> inline common_type_t<T, U> fl_min(T a, U b) {
34 return (a < b) ? a : b;
35}
36
37template <typename T, typename U> inline common_type_t<T, U> fl_max(T a, U b) {
38 return (a > b) ? a : b;
39}
40
42} // namespace fl
43
44#ifndef MAX
45#define MAX(a, b) fl::fl_max(a, b)
46#endif
47
48#ifndef MIN
49#define MIN(a, b) fl::fl_min(a, b)
50#endif
51
52#ifndef ABS
53#define ABS(x) fl::fl_abs(x)
54#endif
55
56#ifndef EPSILON_F
57// smallest possible float
58#define EPSILON_F 1.19209290e-07F
59#endif
60
61#ifndef EPSILON_D
62// smallest possible double
63#define EPSILON_D 2.2204460492503131e-16
64#endif
65
66#ifndef ALMOST_EQUAL
67#define ALMOST_EQUAL(a, b, small) (ABS((a) - (b)) < small)
68#endif
69
70#ifndef ALMOST_EQUAL_FLOAT
71#define ALMOST_EQUAL_FLOAT(a, b) (ABS((a) - (b)) < EPSILON_F)
72#endif
73
74
75
76#ifndef ALMOST_EQUAL_DOUBLE
77#define ALMOST_EQUAL_EPSILON(a, b, epsilon) (ABS((a) - (b)) < (epsilon))
78#endif
79
80#ifndef ALMOST_EQUAL_DOUBLE
81#define ALMOST_EQUAL_DOUBLE(a, b) ALMOST_EQUAL_EPSILON(a, b, EPSILON_F)
82#endif
83
84#ifndef INFINITY_DOUBLE
85#define INFINITY_DOUBLE (1.0 / 0.0)
86#endif
87
88#ifndef INFINITY_FLOAT
89#define INFINITY_FLOAT (1.0f / 0.0f)
90#endif
91
92#ifndef FLT_MAX
93#define FLT_MAX 3.402823466e+38F
94#endif
95
96#ifndef PI
97#define PI 3.1415926535897932384626433832795
98#endif
99
100#ifndef M_PI
101#define M_PI PI
102#endif
#define FL_DISABLE_WARNING(warning)
#define FL_DISABLE_WARNING_IMPLICIT_INT_CONVERSION
#define FL_DISABLE_WARNING_PUSH
#define FL_DISABLE_WARNING_SIGN_CONVERSION
#define FL_DISABLE_WARNING_POP
#define FL_DISABLE_WARNING_FLOAT_CONVERSION
typename common_type< T, U >::type common_type_t
T fl_abs(T value)
Definition math_macros.h:19
FL_DISABLE_WARNING_PUSH U common_type_t< T, U > fl_min(T a, U b)
Definition math_macros.h:33
common_type_t< T, U > fl_max(T a, U b)
Definition math_macros.h:37
IMPORTANT!
Definition crgb.h:20