TDME2  1.9.200
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Math.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/math/fwd-tdme.h>
7 
8 #if defined(_WIN32) && defined(_MSC_VER)
9  #define NOMINMAX
10  #undef max
11  #undef min
12 #endif
13 
14 /**
15  * Standard math functions
16  * @author Andreas Drewke
17  */
18 class tdme::math::Math final
19 {
20 public:
21  static constexpr float PI { 3.141592653589793f };
22  static constexpr float EPSILON { 0.0000001f };
23  static constexpr float DEG2RAD { 0.017453294f };
24  static constexpr float G { 9.80665f };
25 
26  /**
27  * Clamps a value to min or max value
28  * @param value value
29  * @param min min value
30  * @param max max value
31  * @return clamped value
32  */
33  inline static auto clamp(auto value, auto min, auto max) {
34  if (value < min) return min;
35  if (value > max) return max;
36  return value;
37  }
38 
39  /**
40  * Returns sign of value
41  * @param value value
42  * @return -1 if value is negative or +1 if positive
43  */
44  inline static auto sign(auto value) {
45  if (abs(value) < EPSILON) return static_cast<decltype(value)>(1);
46  return value / Math::abs(value);
47  }
48 
49  /**
50  * Do the square product
51  * @param value value
52  * @return square product
53  */
54  inline static auto square(auto value) {
55  return value * value;
56  }
57 
58  /**
59  * Returns absolute value
60  * @param value value
61  * @return absolute value
62  */
63  inline static auto abs(auto value) {
64  return std::abs(value);
65  }
66 
67  /**
68  * Returns the arc cosine of x
69  * @param x x
70  * @return arc cosine of x
71  */
72  inline static float acos(float x) {
73  return std::acos(x);
74  }
75 
76  /**
77  * Returns the arc sine of x
78  * @param x x
79  * @return arc sine of x
80  */
81  inline static float asin(float x) {
82  return std::asin(x);
83  }
84 
85  /**
86  * Returns the arc tangent of x
87  * @param x x
88  * @return arc tangent of x
89  */
90  inline static float atan(float x) {
91  return std::atan(x);
92  }
93 
94  /**
95  * Returns the angle from the conversion of rectangular coordinates to polar coordinates.
96  * @param y y
97  * @param x x
98  * @return arc tangent of y/x
99  */
100  inline static float atan2(float y, float x) {
101  return std::atan2(y, x);
102  }
103 
104  /**
105  * Returns the higher integer value of given value
106  * @param value value
107  * @return higher integer
108  */
109  inline static float ceil(float value) {
110  return std::ceil(value);
111  }
112 
113  /**
114  * Returns the cosine of x
115  * @param x x
116  * @return cosine of x
117  */
118  inline static float cos(float x) {
119  return std::cos(x);
120  }
121 
122  /**
123  * Returns the lower integer value of given value
124  * @param value value
125  * @return lower integer
126  */
127  inline static float floor(float value) {
128  return std::floor(value);
129  }
130 
131  /**
132  * Returns the higher value of given values
133  * @param value1 value 1
134  * @param value2 value 2
135  * @return higher value
136  */
137  inline static auto max(auto value1, auto value2) {
138  return value1 > value2?value1:value2;
139  }
140 
141  /**
142  * Returns the lesser value of given values
143  * @param value1 value 1
144  * @param value2 value 2
145  * @return lesser value
146  */
147  inline static auto min(auto value1, auto value2) {
148  return value1 < value2?value1:value2;
149  }
150 
151  /**
152  * Returns the rounded value of given float value
153  * @param value value
154  * @return rounded value
155  */
156  inline static float round(float value) {
157  return std::round(value);
158  }
159 
160  /**
161  * Returns the value of base raised to the power
162  * @param base base
163  * @param power power
164  * @return base raised to the power
165  */
166  inline static auto pow(auto base, auto power) {
167  return std::pow(base, power);
168  }
169 
170  /**
171  * Returns a random value between 0.0 .. 1.0
172  * @return random value
173  */
174  inline static float random() {
175  return (float)rand() / (float)RAND_MAX;
176  }
177 
178  /**
179  * Returns the sine of x
180  * @param x x
181  * @return sin of x
182  */
183  inline static float sin(float x) {
184  return std::sin(x);
185  }
186 
187  /**
188  * Returns the square root of given value
189  * @param value value
190  * @return square root of value
191  */
192  inline static float sqrt(float value) {
193  return std::sqrt(value);
194  }
195 
196  /**
197  * Returns the tangent of x
198  * @param x x
199  * @return tangent of x
200  */
201  inline static float tan(float x) {
202  return std::tan(x);
203  }
204 
205  /**
206  * Returns e raised to the given power
207  * @param power power
208  * @return e raised to the given power
209  */
210  inline static float exp(float power) {
211  return std::exp(power);
212  }
213 
214  /**
215  * Returns the natural (base e) logarithm of value
216  * @param value value
217  * @return natural (base e) logarithm of value
218  */
219  inline static float log(float value) {
220  return std::log(value);
221  }
222 
223  /**
224  * Returns modulo of value, so that return value is -range < value < range
225  * @param value value
226  * @param range range
227  * @return modulo of value
228  */
229  inline static auto mod(auto value, auto range) {
230  return value % range;
231  }
232 
233  /**
234  * Returns modulo of value, so that return value is -range < value < range
235  * @param value value
236  * @param range range
237  * @return modulo of value
238  */
239  inline static float mod(float value, float range) {
240  return std::fmod(value, range);
241  }
242 
243  /**
244  * Returns absolute modulo of value, so that return value is 0 <= value < range
245  * @param value value
246  * @param range range
247  * @return modulo of value
248  */
249  inline static auto absmod(auto value, auto range) {
250  auto result = value % range;
251  if (result < 0.0f) result+= range;
252  return result;
253  }
254 
255  /**
256  * Returns absolute modulo of value, so that return value is 0.0f <= value < range
257  * @param value value
258  * @param range range
259  * @return modulo of value
260  */
261  inline static float absmod(float value, float range) {
262  auto result = std::fmod(value, range);
263  if (result < 0.0f) result+= range;
264  return result;
265  }
266 
267 };
Standard math functions.
Definition: Math.h:19
static float sqrt(float value)
Returns the square root of given value.
Definition: Math.h:192
static float log(float value)
Returns the natural (base e) logarithm of value.
Definition: Math.h:219
static float round(float value)
Returns the rounded value of given float value.
Definition: Math.h:156
static float sin(float x)
Returns the sine of x.
Definition: Math.h:183
static float tan(float x)
Returns the tangent of x.
Definition: Math.h:201
static float atan(float x)
Returns the arc tangent of x.
Definition: Math.h:90
static auto mod(auto value, auto range)
Returns modulo of value, so that return value is -range < value < range.
Definition: Math.h:229
static auto square(auto value)
Do the square product.
Definition: Math.h:54
static float floor(float value)
Returns the lower integer value of given value.
Definition: Math.h:127
static float cos(float x)
Returns the cosine of x.
Definition: Math.h:118
static constexpr float EPSILON
Definition: Math.h:22
static auto abs(auto value)
Returns absolute value.
Definition: Math.h:63
static float asin(float x)
Returns the arc sine of x.
Definition: Math.h:81
static constexpr float G
Definition: Math.h:24
static float random()
Returns a random value between 0.0 .
Definition: Math.h:174
static auto absmod(auto value, auto range)
Returns absolute modulo of value, so that return value is 0 <= value < range.
Definition: Math.h:249
static auto min(auto value1, auto value2)
Returns the lesser value of given values.
Definition: Math.h:147
static float absmod(float value, float range)
Returns absolute modulo of value, so that return value is 0.0f <= value < range.
Definition: Math.h:261
static float acos(float x)
Returns the arc cosine of x.
Definition: Math.h:72
static auto max(auto value1, auto value2)
Returns the higher value of given values.
Definition: Math.h:137
static float atan2(float y, float x)
Returns the angle from the conversion of rectangular coordinates to polar coordinates.
Definition: Math.h:100
static float exp(float power)
Returns e raised to the given power.
Definition: Math.h:210
static auto pow(auto base, auto power)
Returns the value of base raised to the power.
Definition: Math.h:166
static constexpr float PI
Definition: Math.h:21
static auto sign(auto value)
Returns sign of value.
Definition: Math.h:44
static float mod(float value, float range)
Returns modulo of value, so that return value is -range < value < range.
Definition: Math.h:239
static float ceil(float value)
Returns the higher integer value of given value.
Definition: Math.h:109
static constexpr float DEG2RAD
Definition: Math.h:23
static auto clamp(auto value, auto min, auto max)
Clamps a value to min or max value.
Definition: Math.h:33