GCC Code Coverage Report


libs/base/src/base/
File: numeric.h
Date: 2025-03-19 20:55:25
Lines:
22/22
100.0%
Functions:
10/10
100.0%
Branches:
12/12
100.0%

Line Branch Exec Source
1 #pragma once
2
3 #include "base/ints.h"
4
5
6 namespace eu
7 {
8 namespace kk
9 {
10 constexpr float epsilon = 0.0001f;
11 }
12
13 constexpr float pi = 3.1415926535897932384626433832795f;
14 // constexpr float half_pi = pi / 2.0f;
15
16 438 constexpr float cabs(float r)
17 {
18 return r >= 0.0f
19
2/2
✓ Branch 0 (2 → 3) taken 429 times.
✓ Branch 1 (2 → 4) taken 9 times.
438 ? r
20 438 : -r
21 ;
22 }
23
24 /** \addtogroup math
25 * @{
26 */
27 414 constexpr bool is_zero(float r, float epsilon = kk::epsilon)
28 {
29 414 return cabs(r) < epsilon;
30 }
31
32 constexpr bool
33 345 is_equal(float lhs, float rhs, float epsilon = kk::epsilon)
34 {
35 345 return is_zero(lhs - rhs, epsilon);
36 }
37
38 int
39 floor_to_int(float v);
40
41 int
42 ceil_to_int(float v);
43
44
45 /** Calculates the sign as a positive or a negative int.
46 @returns 1 if r is greater than 0, -1 if not.
47 */
48 constexpr int
49 3 get_sign(float r)
50 {
51
2/2
✓ Branch 0 (2 → 3) taken 2 times.
✓ Branch 1 (2 → 4) taken 1 times.
3 if(r >= 0.0f) { return 1; }
52 1 else { return -1; }
53 }
54
55
56 /// Returns `1` if `true` or -1
57 constexpr float
58 2 get_sign(bool b)
59 {
60
2/2
✓ Branch 0 (2 → 3) taken 1 times.
✓ Branch 1 (2 → 4) taken 1 times.
2 if(b) { return 1.0f; }
61 1 else { return -1.0f; }
62 }
63
64
65 constexpr float
66 8 lerp_float(float f, float scale, float t)
67 {
68 8 return f + (t - f) * scale;
69 }
70
71 /// Return `r * r`
72 constexpr float
73 1 square(float r)
74 {
75 1 return r * r;
76 }
77
78
2/2
✓ Branch 0 (2 → 3) taken 1 times.
✓ Branch 1 (2 → 4) taken 1 times.
2 constexpr float min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
79
2/2
✓ Branch 0 (2 → 3) taken 1 times.
✓ Branch 1 (2 → 4) taken 1 times.
2 constexpr float max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
80
81
82 /// If the `value` is close to zero, `def` is returned
83 constexpr float
84 12 clamp_zero(float value, float def = 0.0f, float epsilon = kk::epsilon)
85 {
86
2/2
✓ Branch 0 (3 → 4) taken 5 times.
✓ Branch 1 (3 → 5) taken 7 times.
12 if(is_zero(value, epsilon)) { return def; }
87 7 else { return value; }
88 }
89
90 /** @}*/
91 }
92
93