jaulib: include/jau/base_math.hpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifndef JAU_BASE_MATH_HPP_
26#define JAU_BASE_MATH_HPP_
27
28#include
29#include
30#include
31#include <type_traits>
32
36
37namespace jau {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 template<jau::req::arithmetic T>
63 bool in_range(const T& a, const T& b, const T& range) {
64 return std::abs(a-b) <= range;
65 }
66
67
68 template
73
74 template
75 requires std::floating_point
79
80 template
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 template <jau::req::signed_arithmetic T>
102 constexpr int sign(const T x) noexcept
103 {
104 return (int) ( (T(0) < x) - (x < T(0)) );
105 }
106
107 template <jau::req::unsigned_arithmetic T>
108 constexpr int sign(const T x) noexcept
109 {
110 return (int) ( T(0) < x );
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 template <jau::req::signed_arithmetic T>
141 {
142 return std::numeric_limits::min() == x ? std::numeric_limits::max() : -x;
143 }
144
145 template <jau::req::unsigned_arithmetic T>
146 constexpr T invert_sign(const T x) noexcept
147 {
148 return x;
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 template <jau::req::signed_arithmetic T>
165 constexpr T abs(const T x) noexcept
166 {
168 }
169
170 template <jau::req::unsigned_arithmetic T>
171 constexpr T abs(const T x) noexcept
172 {
173 return x;
174 }
175
176
177
178
179
180
181
182
183
184
185 template<jau::req::signed_arithmetic T>
186 constexpr std::make_unsigned_t unsigned_value(const T x) noexcept {
187 using U = std::make_unsigned_t;
189 if (std::numeric_limits::min() == x) {
190 return U(std::numeric_limits::max()) + U(1);
191 } else {
192 return U(-x);
193 }
194 } else {
195 return U(x);
196 }
197 }
198
199 template <jau::req::unsigned_arithmetic T>
201 {
202 return x;
203 }
204
205
206
207
208
209
210
211
212 template <jau::req::arithmetic T>
213 constexpr T min(const T x, const T y) noexcept
214 {
215 return x < y ? x : y;
216 }
217
218
219
220
221
222
223
224
225 template <jau::req::arithmetic T>
226 constexpr T max(const T x, const T y) noexcept
227 {
228 return x > y ? x : y;
229 }
230
231
232
233
234
235
236
237
238
239
240
241 template <jau::req::arithmetic T>
242 constexpr T clamp(const T x, const T min_val, const T max_val) noexcept
243 {
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259 template <jau::req::arithmetic R, jau::req::arithmetic T>
260 constexpr R clampCast(const T x, const T min_val, const T max_val) noexcept
261 {
262 return static_cast<R>(jau::clamp(x, min_val, max_val));
263 }
264
265
266
267}
268
269#endif
Concept of type-trait std::is_signed and std::is_integral.
Concept of type-trait std::is_unsigned and std::is_integral.
constexpr T invert_sign(const T x) noexcept
Safely inverts the sign of an arithmetic number w/ branching in O(1)
Definition base_math.hpp:140
constexpr T clamp(const T x, const T min_val, const T max_val) noexcept
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1)...
Definition base_math.hpp:242
constexpr R clampCast(const T x, const T min_val, const T max_val) noexcept
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1)...
Definition base_math.hpp:260
constexpr int sign(const T x) noexcept
Returns the value of the sign function (w/o branching ?) in O(1).
Definition base_math.hpp:102
constexpr bool is_positive(const T a) noexcept
Returns true of the given integral is positive, i.e.
Definition base_math.hpp:70
constexpr std::make_unsigned_t< T > unsigned_value(const T x) noexcept
Returns the unsigned typed absolute value of an arithmetic number (w/ branching) in O(1)
Definition base_math.hpp:186
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
Definition base_math.hpp:213
bool in_range(const T &a, const T &b, const T &range)
base_math: arithmetic types, i.e.
Definition base_math.hpp:63
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
Definition base_math.hpp:226
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
Definition base_math.hpp:165
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.