Plasma Engine  2.0
Loading...
Searching...
No Matches
Constants_inl.h
1#pragma once
2
3#include <float.h>
4
5namespace plMath
6{
8
9 template <>
10 constexpr float Pi()
11 {
12 return static_cast<float>(3.1415926535897932384626433832795f);
13 }
14
15 template <>
16 constexpr double Pi()
17 {
18 return static_cast<double>(3.1415926535897932384626433832795);
19 }
20
22
23 template <>
24 constexpr float e()
25 {
26 return static_cast<float>(2.71828182845904);
27 }
28
29 template <>
30 constexpr double e()
31 {
32 return static_cast<double>(2.71828182845904);
33 }
34
36
37 template <typename TYPE>
38 constexpr bool SupportsNaN()
39 {
40 return false;
41 }
42
43 template <>
44 constexpr bool SupportsNaN<float>()
45 {
46 return true;
47 }
48
49 template <>
50 constexpr bool SupportsNaN<double>()
51 {
52 return true;
53 }
54
56
57 template <typename TYPE>
58 constexpr TYPE NaN()
59 {
60 return static_cast<TYPE>(0);
61 }
62
63 template <>
64 constexpr float NaN()
65 {
66 // NAN -> (exponent = all 1, mantissa = non-zero)
67 // INF -> (exponent = all 1, mantissa = zero)
68
69 // NaN = 0111 1111 1000 0000 0000 0000 0000 0001
70
71 plIntFloatUnion i2f(0x7f800042u);
72 return i2f.f;
73 }
74
75 template <>
76 constexpr double NaN()
77 {
78 // NAN -> (exponent = all 1, mantissa = non-zero)
79 // INF -> (exponent = all 1, mantissa = zero)
80
81 // NaN = 0111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001
82
83 plInt64DoubleUnion i2f(0x7FF0000000000042ull);
84 return i2f.f;
85 }
86
88
89 template <typename TYPE>
90 constexpr bool SupportsInfinity()
91 {
92 return false;
93 }
94
95 template <>
96 constexpr bool SupportsInfinity<float>()
97 {
98 return true;
99 }
100
101 template <>
102 constexpr bool SupportsInfinity<double>()
103 {
104 return true;
105 }
106
108
109 template <typename TYPE>
110 constexpr TYPE Infinity()
111 {
112 return static_cast<TYPE>(0);
113 }
114
115 template <>
116 constexpr float Infinity()
117 {
118 // NAN -> (exponent = all 1, mantissa = non-zero)
119 // INF -> (exponent = all 1, mantissa = zero)
120
121 // INF = 0111 1111 1000 0000 0000 0000 0000 0000
122
123 // bitwise representation of float infinity (positive)
124 plIntFloatUnion i2f(0x7f800000u);
125 return i2f.f;
126 }
127
128 template <>
129 constexpr double Infinity()
130 {
131 // NAN -> (exponent = all 1, mantissa = non-zero)
132 // INF -> (exponent = all 1, mantissa = zero)
133
134 // INF = 0111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
135
136 // bitwise representation of double infinity (positive)
137 plInt64DoubleUnion i2f(0x7FF0000000000000ull);
138
139 return i2f.f;
140 }
141
143
144 template <>
145 constexpr plUInt8 MaxValue()
146 {
147 return 0xFF;
148 }
149
150 template <>
151 constexpr plUInt16 MaxValue()
152 {
153 return 0xFFFF;
154 }
155
156 template <>
157 constexpr plUInt32 MaxValue()
158 {
159 return 0xFFFFFFFFu;
160 }
161
162 template <>
163 constexpr plUInt64 MaxValue()
164 {
165 return 0xFFFFFFFFFFFFFFFFull;
166 }
167
168 template <>
169 constexpr plInt8 MaxValue()
170 {
171 return 0x7F;
172 }
173
174 template <>
175 constexpr plInt16 MaxValue()
176 {
177 return 0x7FFF;
178 }
179
180 template <>
181 constexpr plInt32 MaxValue()
182 {
183 return 0x7FFFFFFF;
184 }
185
186 template <>
187 constexpr plInt64 MaxValue()
188 {
189 return 0x7FFFFFFFFFFFFFFFll;
190 }
191
192 template <>
193 constexpr float MaxValue()
194 {
195 return 3.402823465e+38F;
196 }
197
198 template <>
199 constexpr double MaxValue()
200 {
201 return 1.7976931348623158e+307;
202 }
203
205
206 template <>
207 constexpr plUInt8 MinValue()
208 {
209 return 0;
210 }
211
212 template <>
213 constexpr plUInt16 MinValue()
214 {
215 return 0;
216 }
217
218 template <>
219 constexpr plUInt32 MinValue()
220 {
221 return 0;
222 }
223
224 template <>
225 constexpr plUInt64 MinValue()
226 {
227 return 0;
228 }
229
230 template <>
231 constexpr plInt8 MinValue()
232 {
233 return -MaxValue<plInt8>() - 1;
234 }
235
236 template <>
237 constexpr plInt16 MinValue()
238 {
239 return -MaxValue<plInt16>() - 1;
240 }
241
242 template <>
243 constexpr plInt32 MinValue()
244 {
245 return -MaxValue<plInt32>() - 1;
246 }
247
248 template <>
249 constexpr plInt64 MinValue()
250 {
251 return -MaxValue<plInt64>() - 1;
252 }
253
254 template <>
255 constexpr float MinValue()
256 {
257 return -3.402823465e+38F;
258 }
259
260 template <>
261 constexpr double MinValue()
262 {
263 return -1.7976931348623158e+307;
264 }
265
267
268 template <>
269 constexpr float HighValue()
270 {
271 return 1.8446726e+019f;
272 }
273
274 template <>
275 constexpr double HighValue()
276 {
277 return 1.8446726e+150;
278 }
279
281
282 template <>
283 constexpr float FloatEpsilon()
284 {
285 return FLT_EPSILON;
286 }
287
288 template <>
289 constexpr double FloatEpsilon()
290 {
291 return DBL_EPSILON;
292 }
293
294
295 template <typename TYPE>
296 constexpr TYPE SmallEpsilon()
297 {
298 return (TYPE)0.000001;
299 }
300
301 template <typename TYPE>
302 constexpr TYPE DefaultEpsilon()
303 {
304 return (TYPE)0.00001;
305 }
306
307 template <typename TYPE>
308 constexpr TYPE LargeEpsilon()
309 {
310 return (TYPE)0.0001;
311 }
312
313 template <typename TYPE>
314 constexpr TYPE HugeEpsilon()
315 {
316 return (TYPE)0.001;
317 }
318
320
321 template <>
322 constexpr plUInt32 NumBits<plUInt8>()
323 {
324 return 8;
325 }
326
327 template <>
328 constexpr plUInt32 NumBits<plUInt16>()
329 {
330 return 16;
331 }
332
333 template <>
334 constexpr plUInt32 NumBits<plUInt32>()
335 {
336 return 32;
337 }
338
339 template <>
340 constexpr plUInt32 NumBits<plUInt64>()
341 {
342 return 64;
343 }
344
345 template <>
346 constexpr plUInt32 NumBits<plInt8>()
347 {
348 return 8;
349 }
350
351 template <>
352 constexpr plUInt32 NumBits<plInt16>()
353 {
354 return 16;
355 }
356
357 template <>
358 constexpr plUInt32 NumBits<plInt32>()
359 {
360 return 32;
361 }
362
363 template <>
364 constexpr plUInt32 NumBits<plInt64>()
365 {
366 return 64;
367 }
368
369 template <>
370 constexpr plUInt32 NumBits<float>()
371 {
372 return 32;
373 }
374
375 template <>
376 constexpr plUInt32 NumBits<double>()
377 {
378 return 64;
379 }
380
382
383} // namespace plMath
This namespace provides common math-functionality as functions.
Definition Constants.h:6
constexpr TYPE Pi()
Returns the natural constant Pi.
constexpr TYPE Infinity()
Returns the value for Infinity as the template type. Returns zero, if the type does not support Infin...
Definition Constants_inl.h:110
constexpr bool SupportsInfinity()
Returns whether the template type supports specialized values to represent Infinity.
Definition Constants_inl.h:90
constexpr TYPE FloatEpsilon()
The difference between 1.0 and the next representable value for the given type.
constexpr TYPE MaxValue()
Returns the largest possible positive value (that is not infinity).
constexpr TYPE HighValue()
A very large value, that is slightly smaller than sqrt(MaxValue()).
constexpr TYPE NaN()
Returns the value for NaN as the template type. Returns zero, if the type does not support NaN.
Definition Constants_inl.h:58
constexpr TYPE e()
Returns the natural constant e.
constexpr TYPE MinValue()
Returns the smallest possible value (that is not -infinity). Usually zero or -MaxValue()....
constexpr bool SupportsNaN()
Returns whether the template type supports specialized values to represent NaN.
Definition Constants_inl.h:38
Simple helper union to store ints and doubles to modify their bit patterns.
Definition Declarations.h:35
Simple helper union to store ints and floats to modify their bit patterns.
Definition Declarations.h:18