GCC Code Coverage Report


Directory: src/gate/
File: src/gate/mathematics.hpp
Date: 2026-09-21 06:26:40
Exec Total Coverage
Lines: 14 14 100.0%
Functions: 8 8 100.0%
Branches: 2 4 50.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 /** @file
30 * @brief Basic arithmetic utilities optimized for GATE types
31 * @ingroup gatecore_cpp
32 */
33
34 #ifndef GATE_MATHEMATICS_HPP_INCLUDED
35 #define GATE_MATHEMATICS_HPP_INCLUDED
36
37 #include "gate/gate_core_api.hpp"
38 #include "gate/gatetypes.hpp"
39 #include "gate/typetraits.hpp"
40 #include "gate/mathematics.h"
41
42 namespace gate
43 {
44
45 namespace math
46 {
47
48 GATE_CORE_CPP_API extern real64_t const PI;
49 GATE_CORE_CPP_API extern real64_t const E;
50
51
52 /// @brief
53 /// @tparam T
54 /// @param num
55 /// @return
56 3 template<class T> bool_t isZero(T num)
57 {
58 3 return num == T(0);
59 }
60
61
62 /// @brief
63 /// @param num
64 /// @return
65 GATE_CORE_CPP_API bool_t isZero(real32_t num);
66
67
68 /// @brief
69 /// @param num
70 /// @return
71 GATE_CORE_CPP_API bool_t isZero(real64_t num);
72
73
74 /// @brief
75 /// @tparam T
76 /// @param num
77 /// @return
78 1 template<class T> bool_t isNan(T num)
79 {
80 1 return !traits::IsNumeric<T>::value;
81 }
82
83
84 /// @brief
85 /// @param num
86 /// @return
87 GATE_CORE_CPP_API bool_t isNan(real32_t num);
88
89
90 /// @brief
91 /// @param num
92 /// @return
93 GATE_CORE_CPP_API bool_t isNan(real64_t num);
94
95
96 /// @brief
97 /// @param num
98 /// @return
99 GATE_CORE_CPP_API void buildNan(real32_t& num);
100
101
102 /// @brief
103 /// @param num
104 /// @return
105 GATE_CORE_CPP_API void buildNan(real64_t& num);
106
107
108 /// @brief
109 /// @tparam T
110 /// @param num
111 /// @return
112 1 template<class T> bool_t isInfinite(T num)
113 {
114 1 return false;
115 }
116
117
118 /// @brief
119 /// @param num
120 /// @return
121 GATE_CORE_CPP_API bool_t isInfinite(real32_t num);
122
123
124 /// @brief
125 /// @param num
126 /// @return
127 GATE_CORE_CPP_API bool_t isInfinite(real64_t num);
128
129
130 /// @brief
131 /// @tparam T
132 /// @param num
133 /// @return
134 template<class T> int signum(T num)
135 {
136 return isZero(num) ? 0 : ((num < T(0)) ? -1 : 1);
137 }
138
139
140 /// @brief
141 /// @param num
142 /// @return
143 GATE_CORE_CPP_API int signum(int8_t num);
144
145
146 /// @brief
147 /// @param num
148 /// @return
149 GATE_CORE_CPP_API int signum(int16_t num);
150
151
152 /// @brief
153 /// @param num
154 /// @return
155 GATE_CORE_CPP_API int signum(int32_t num);
156
157
158 /// @brief
159 /// @param num
160 /// @return
161 GATE_CORE_CPP_API int signum(int64_t num);
162
163
164 /// @brief
165 /// @param num
166 /// @return
167 GATE_CORE_CPP_API int signum(real32_t num);
168
169
170 /// @brief
171 /// @param num
172 /// @return
173 GATE_CORE_CPP_API int signum(real64_t num);
174
175
176 /// @brief
177 /// @param num
178 /// @return
179 GATE_CORE_CPP_API int8_t abs(int8_t num);
180
181
182 /// @brief
183 /// @param num
184 /// @return
185 GATE_CORE_CPP_API int16_t abs(int16_t num);
186
187
188 /// @brief
189 /// @param num
190 /// @return
191 GATE_CORE_CPP_API int32_t abs(int32_t num);
192
193
194 /// @brief
195 /// @param num
196 /// @return
197 GATE_CORE_CPP_API int64_t abs(int64_t num);
198
199
200 /// @brief
201 /// @param num
202 /// @return
203 GATE_CORE_CPP_API real32_t abs(real32_t num);
204
205
206 /// @brief
207 /// @param num
208 /// @return
209 GATE_CORE_CPP_API real64_t abs(real64_t num);
210
211
212 #if !defined(GATE_COMPILER_SUPPORTS_CPP_PARTIAL_TEMPLATE_SPECIALIZATION)
213
214 template<class T> T abs(T num)
215 {
216 if (num < T(0))
217 {
218 return -num;
219 }
220 return num;
221 }
222
223 #else
224
225 namespace
226 {
227
228 template<class T, bool SIGN> struct AbsImpl;
229
230 template<class T> struct AbsImpl<T, true>
231 {
232 static T abs(T num)
233 {
234 if (num < T(0))
235 {
236 return -num;
237 }
238 else
239 {
240 return num;
241 }
242 }
243 };
244 template<class T> struct AbsImpl<T, false>
245 {
246 1 static T abs(T num)
247 {
248 1 return num;
249 }
250 };
251 } // end of anonymous namespace
252
253
254 /// @brief
255 /// @tparam T
256 /// @param num
257 /// @return
258 1 template<class T> T abs(T num)
259 {
260 1 return AbsImpl<T, gate::traits::IsSigned<T>::value>::abs(num);
261 }
262 #endif
263
264 /// @brief
265 /// @tparam T
266 /// @param num1
267 /// @param num2
268 /// @return
269 1 template<class T> T minimum(T num1, T num2)
270 {
271
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return num1 < num2 ? num1 : num2;
272 }
273
274
275 /// @brief
276 /// @tparam T
277 /// @param num1
278 /// @param num2
279 /// @return
280 1 template<class T> T maximum(T num1, T num2)
281 {
282
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return num1 < num2 ? num2 : num1;
283 }
284
285
286 /// @brief
287 /// @tparam T
288 /// @param num
289 /// @return
290 template<class T> uint8_t decimal_length(T num);
291 GATE_CORE_CPP_API uint8_t decimal_length(real32_t num);
292 GATE_CORE_CPP_API uint8_t decimal_length(real64_t num);
293
294
295 /// @brief
296 /// @tparam T
297 /// @param num
298 /// @return
299 template<class T> T cos(T num);
300 GATE_CORE_CPP_API real32_t cos(real32_t num);
301 GATE_CORE_CPP_API real64_t cos(real64_t num);
302
303
304 /// @brief
305 /// @tparam T
306 /// @param num
307 /// @return
308 template<class T> T sin(T num);
309 GATE_CORE_CPP_API real32_t sin(real32_t num);
310 GATE_CORE_CPP_API real64_t sin(real64_t num);
311
312
313 /// @brief
314 /// @tparam T
315 /// @param num
316 /// @return
317 template<class T> T tan(T num);
318 GATE_CORE_CPP_API real32_t tan(real32_t num);
319 GATE_CORE_CPP_API real64_t tan(real64_t num);
320
321
322 /// @brief
323 /// @tparam T
324 /// @param num
325 /// @return
326 template<class T> T acos(T num);
327 GATE_CORE_CPP_API real32_t acos(real32_t num);
328 GATE_CORE_CPP_API real64_t acos(real64_t num);
329
330
331 /// @brief
332 /// @tparam T
333 /// @param num
334 /// @return
335 template<class T> T asin(T num);
336 GATE_CORE_CPP_API real32_t asin(real32_t num);
337 GATE_CORE_CPP_API real64_t asin(real64_t num);
338
339
340 /// @brief
341 /// @tparam T
342 /// @param num
343 /// @return
344 template<class T> T atan(T num);
345 GATE_CORE_CPP_API real32_t atan(real32_t num);
346 GATE_CORE_CPP_API real64_t atan(real64_t num);
347
348
349 /// @brief
350 /// @tparam T
351 /// @param y
352 /// @param x
353 /// @return
354 template<class T> T atan2(T y, T x);
355 GATE_CORE_CPP_API real32_t atan2(real32_t y, real32_t x);
356 GATE_CORE_CPP_API real64_t atan2(real64_t y, real64_t x);
357
358
359 /// @brief
360 /// @tparam T
361 /// @param num
362 /// @return
363 template<class T> T cosh(T num);
364 GATE_CORE_CPP_API real32_t cosh(real32_t num);
365 GATE_CORE_CPP_API real64_t cosh(real64_t num);
366
367
368 /// @brief
369 /// @tparam T
370 /// @param num
371 /// @return
372 template<class T> T sinh(T num);
373 GATE_CORE_CPP_API real32_t sinh(real32_t num);
374 GATE_CORE_CPP_API real64_t sinh(real64_t num);
375
376
377 /// @brief
378 /// @tparam T
379 /// @param num
380 /// @return
381 template<class T> T tanh(T num);
382 GATE_CORE_CPP_API real32_t tanh(real32_t num);
383 GATE_CORE_CPP_API real64_t tanh(real64_t num);
384
385
386 /// @brief
387 /// @tparam T
388 /// @param num
389 /// @return
390 template<class T> T acosh(T num);
391 GATE_CORE_CPP_API real32_t acosh(real32_t num);
392 GATE_CORE_CPP_API real64_t acosh(real64_t num);
393
394
395
396 /// @brief
397 /// @tparam T
398 /// @param num
399 /// @return
400 template<class T> T asinh(T num);
401 GATE_CORE_CPP_API real32_t asinh(real32_t num);
402 GATE_CORE_CPP_API real64_t asinh(real64_t num);
403
404
405
406 /// @brief
407 /// @tparam T
408 /// @param num
409 /// @return
410 template<class T> T atanh(T num);
411 GATE_CORE_CPP_API real32_t atanh(real32_t num);
412 GATE_CORE_CPP_API real64_t atanh(real64_t num);
413
414
415 /// @brief
416 /// @tparam T
417 /// @param num
418 /// @return
419 template<class T> T exp(T num);
420 GATE_CORE_CPP_API real32_t exp(real32_t num);
421 GATE_CORE_CPP_API real64_t exp(real64_t num);
422
423
424 /// @brief
425 /// @tparam T
426 /// @param num
427 /// @param exp
428 /// @return
429 template<class T> T frexp(T num, int* exp);
430 GATE_CORE_CPP_API real32_t frexp(real32_t num, int* exp);
431 GATE_CORE_CPP_API real64_t frexp(real64_t num, int* exp);
432
433
434 /// @brief
435 /// @tparam T
436 /// @param num
437 /// @param exp
438 /// @return
439 template<class T> T ldexp(T num, int exp);
440 GATE_CORE_CPP_API real32_t ldexp(real32_t num, int exp);
441 GATE_CORE_CPP_API real64_t ldexp(real64_t num, int exp);
442
443
444 /// @brief
445 /// @tparam T
446 /// @param num
447 /// @return
448 template<class T> T log(T num);
449 GATE_CORE_CPP_API real32_t log(real32_t num);
450 GATE_CORE_CPP_API real64_t log(real64_t num);
451
452
453
454 /// @brief
455 /// @tparam T
456 /// @param num
457 /// @return
458 template<class T> T log10(T num);
459 GATE_CORE_CPP_API real32_t log10(real32_t num);
460 GATE_CORE_CPP_API real64_t log10(real64_t num);
461
462
463 /// @brief
464 /// @tparam T
465 /// @param num
466 /// @param exp
467 /// @return
468 template<class T> T modf(T num, T* exp);
469 GATE_CORE_CPP_API real32_t modf(real32_t num, real32_t* exp);
470 GATE_CORE_CPP_API real64_t modf(real64_t num, real64_t* exp);
471
472
473 /// @brief
474 /// @tparam T
475 /// @param num
476 /// @param exp
477 /// @return
478 template<class T> T pow(T num, T exp);
479 GATE_CORE_CPP_API real32_t pow(real32_t num, real32_t exp);
480 GATE_CORE_CPP_API real64_t pow(real64_t num, real64_t exp);
481
482
483 /// @brief
484 /// @tparam T
485 /// @param num
486 /// @return
487 template<class T> T sqrt(T num);
488 GATE_CORE_CPP_API real32_t sqrt(real32_t num);
489 GATE_CORE_CPP_API real64_t sqrt(real64_t num);
490
491
492 /// @brief
493 /// @tparam T
494 /// @param num
495 /// @return
496 template<class T> T ceil(T num);
497 GATE_CORE_CPP_API real32_t ceil(real32_t num);
498 GATE_CORE_CPP_API real64_t ceil(real64_t num);
499
500
501 /// @brief
502 /// @tparam T
503 /// @param num
504 /// @return
505 template<class T> T floor(T num);
506 GATE_CORE_CPP_API real32_t floor(real32_t num);
507 GATE_CORE_CPP_API real64_t floor(real64_t num);
508
509
510 /// @brief
511 /// @tparam T
512 /// @param num
513 /// @return
514 template<class T> T deg2rad(T num);
515 GATE_CORE_CPP_API real32_t deg2rad(real32_t num);
516 GATE_CORE_CPP_API real64_t deg2rad(real64_t num);
517
518
519 /// @brief
520 /// @tparam T
521 /// @param num
522 /// @return
523 template<class T> T rad2deg(T num);
524 GATE_CORE_CPP_API real32_t rad2deg(real32_t num);
525 GATE_CORE_CPP_API real64_t rad2deg(real64_t num);
526
527
528 } // end of namespace math
529
530 } // end of namespace gate
531
532 #endif
533