jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
float_math.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020-2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef JAU_FLOAT_MATH_HPP_
26#define JAU_FLOAT_MATH_HPP_
27
28#include <cmath>
29#include <climits>
30#include <type_traits>
31
32#include <jau/base_math.hpp>
33#include <jau/string_util.hpp>
34
35namespace jau {
36 /** @defgroup Floats Float types and arithmetic
37 * Float types and arithmetic, see also and meta-group \ref Math
38 * @{
39 */
40
41 /**
42 * base_math: arithmetic types, i.e. integral + floating point types
43 * int_math: integral types
44 * float_math: floating point types
45 // *************************************************
46 // *************************************************
47 // *************************************************
48 */
49
50 using namespace jau::int_literals;
51
52 typedef typename jau::uint_bytes_t<sizeof(float)> float_uint_t;
53 typedef typename jau::uint_bytes_t<sizeof(double)> double_uint_t;
54
55 /** Signed bit 31 of IEEE 754 (IEC 559) single float-point bit layout, i.e. `0x80000000`. */
56 constexpr uint32_t const float_iec559_sign_bit = 1_u32 << 31; // 0x80000000_u32;
57
58 /** Exponent mask bits 23-30 of IEEE 754 (IEC 559) single float-point bit layout, i.e. `0x7f800000`. */
59 constexpr uint32_t const float_iec559_exp_mask = 0x7f800000_u32;
60
61 /** Mantissa mask bits 0-22 of IEEE 754 (IEC 559) single float-point bit layout, i.e. `0x007fffff`. */
62 constexpr uint32_t const float_iec559_mant_mask = 0x007fffff_u32;
63
64 /** Positive infinity bit-value of IEEE 754 (IEC 559) single float-point bit layout, i.e. `0x7f800000`. */
65 constexpr uint32_t const float_iec559_positive_inf_bitval = 0x7f800000_u32;
66
67 /** Negative infinity bit-value of IEEE 754 (IEC 559) single float-point bit layout, i.e. `0xff800000`. */
68 constexpr uint32_t const float_iec559_negative_inf_bitval = 0xff800000_u32;
69
70 /** NaN bit-value of IEEE 754 (IEC 559) single float-point bit layout, i.e. `0x7fc00000`. */
71 constexpr uint32_t const float_iec559_nan_bitval = 0x7fc00000_u32;
72
73 /** Signed bit 63 of IEEE 754 (IEC 559) double double-point bit layout, i.e. `0x8000000000000000`. */
74 constexpr uint64_t const double_iec559_sign_bit = 1_u64 << 63; // 0x8000000000000000_u64;
75
76 /** Exponent mask bits 52-62 of IEEE 754 (IEC 559) double double-point bit layout, i.e. `0x7ff0000000000000`. */
77 constexpr uint64_t const double_iec559_exp_mask = 0x7ff0000000000000_u64;
78
79 /** Mantissa mask bits 0-51 of IEEE 754 (IEC 559) double double-point bit layout, i.e. `0x000fffffffffffff`. */
80 constexpr uint64_t const double_iec559_mant_mask = 0x000fffffffffffff_u64;
81
82 /** Positive infinity bit-value of IEEE 754 (IEC 559) double double-point bit layout, i.e. `0x7ff0000000000000`. */
83 constexpr uint64_t const double_iec559_positive_inf_bitval = 0x7ff0000000000000_u64;
84
85 /** Negative infinity bit-value of IEEE 754 (IEC 559) double double-point bit layout, i.e. `0xfff0000000000000`. */
86 constexpr uint64_t const double_iec559_negative_inf_bitval = 0xfff0000000000000_u64;
87
88 /** NaN bit-value of IEEE 754 (IEC 559) double double-point bit layout, i.e. `0x7ff8000000000000`. */
89 constexpr uint64_t const double_iec559_nan_bitval = 0x7ff8000000000000_u64;
90
91 /**
92 * Returns the unsigned integer representation
93 * according to IEEE 754 (IEC 559) floating-point bit layout.
94 *
95 * Meaningful semantics are only given if `true == std::numeric_limits<T>::is_iec559`.
96 *
97 * This raw method does not collapse all NaN values.
98 *
99 * The result is a functional unsigned integer that,
100 * i.e. reversible to double via float_value() or double via double_value() depending on type `T`,
101 *
102 * See specific semantics of IEEE 754 (IEC 559) single floating-point bit layout:
103 * - float_iec559_sign_bit
104 * - float_iec559_exp_mask
105 * - float_iec559_mant_mask
106 * - float_iec559_positive_inf_bitval
107 * - float_iec559_negative_inf_bitval
108 * - float_iec559_nan_bitval
109 *
110 * See specific semantics of IEEE 754 (IEC 559) double doubleing-point bit layout:
111 * - double_iec559_sign_bit
112 * - double_iec559_exp_mask
113 * - double_iec559_mant_mask
114 * - double_iec559_positive_inf_bitval
115 * - double_iec559_negative_inf_bitval
116 * - double_iec559_nan_bitval
117 *
118 * @tparam T floating point type, e.g. float or double
119 * @tparam matching floating point unsigned integer type, e.g. float_uint_t or double_uint_t
120 * @param a float value
121 * @return unsigned integer representation of IEEE 754 (IEC 559) floating-point bit layout
122 * @see float_value()
123 * @see double_value()
124 */
125 template<class T,
126 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
127 typename jau::uint_bytes_t<sizeof(T)>
128 bit_value_raw(const T a) noexcept
129 {
130 typedef typename jau::uint_bytes_t<sizeof(T)> T_uint;
131 union { T_uint u; T f; } iec559 = { .f = a };
132 return iec559.u;
133 }
134
135 /**
136 * Returns the unsigned integer representation
137 * according to IEEE 754 (IEC 559) single floating-point bit layout.
138 *
139 * See bit_value() for details.
140 *
141 * This raw method does not collapse all NaN values to float_iec559_nan_bitval.
142 */
143 constexpr uint32_t bit_value_raw(const float a) noexcept {
144 union { uint32_t u; float f; } iec559 = { .f = a };
145 return iec559.u;
146 }
147 /**
148 * Returns the unsigned integer representation
149 * according to IEEE 754 (IEC 559) single floating-point bit layout.
150 *
151 * Meaningful semantics are only given if `true == std::numeric_limits<float>::is_iec559`.
152 *
153 * All NaN values which are represented by float_iec559_nan_bitval.
154 *
155 * The result is a functional unsigned integer that, i.e. reversible to double via float_value().
156 *
157 * See specific semantics of IEEE 754 (IEC 559) single floating-point bit layout:
158 * - float_iec559_sign_bit
159 * - float_iec559_exp_mask
160 * - float_iec559_mant_mask
161 * - float_iec559_positive_inf_bitval
162 * - float_iec559_negative_inf_bitval
163 * - float_iec559_nan_bitval
164 *
165 * The result is a functional unsigned integer that, i.e. reversible to float via float_value(),
166 * except all NaN values which are represented by float_iec559_nan_bitval.
167 *
168 * @param a single float value
169 * @return unsigned integer representation of IEEE 754 (IEC 559) single floating-point bit layout
170 * @see float_value()
171 * @see bit_value_raw()
172 */
173 constexpr uint32_t bit_value(const float a) noexcept {
174 if( std::isnan(a) ) {
176 }
177 return bit_value_raw(a);
178 }
179
180 /** Converting IEEE 754 (IEC 559) single floating-point bit layout to float, see bit_value() */
181 constexpr float float_value(const uint32_t a) noexcept {
182 union { uint32_t u; float f; } iec559 = { .u = a };
183 return iec559.f;
184 }
185 /**
186 * Returns the unsigned integer representation
187 * according to IEEE 754 (IEC 559) double floating-point bit layout.
188 *
189 * See bit_value() for details.
190 *
191 * This raw method does not collapse all NaN values to double_iec559_nan_bitval.
192 */
193 constexpr uint64_t bit_value_raw(const double a) noexcept {
194 union { uint64_t u; double f; } iec559 = { .f = a };
195 return iec559.u;
196 }
197 /**
198 * Returns the unsigned integer representation
199 * according to IEEE 754 (IEC 559) double floating-point bit layout.
200 *
201 * Meaningful semantics are only given if `true == std::numeric_limits<double>::is_iec559`.
202 *
203 * All NaN values which are represented by double_iec559_nan_bitval.
204 *
205 * The result is a functional unsigned integer that, i.e. reversible to double via double_value().
206 *
207 * See specific semantics of IEEE 754 (IEC 559) double floating-point bit layout:
208 * - double_iec559_sign_bit
209 * - double_iec559_exp_mask
210 * - double_iec559_mant_mask
211 * - double_iec559_positive_inf_bitval
212 * - double_iec559_negative_inf_bitval
213 * - double_iec559_nan_bitval
214 *
215 * @param a double float value
216 * @return unsigned integer representation of IEEE 754 (IEC 559) double floating-point bit layout
217 * @see double_value()
218 * @see bit_value_raw()
219 */
220 constexpr uint64_t bit_value(const double a) noexcept {
221 if( std::isnan(a) ) {
223 }
224 return bit_value_raw(a);
225 }
226 /** Converting IEEE 754 (IEC 559) double floating-point bit layout to double, see bit_value() */
227 constexpr double double_value(const uint64_t a) noexcept {
228 union { uint64_t u; double f; } iec559 = { .u = a };
229 return iec559.f;
230 }
231
232 /**
233 * Calculates the smallest floating point value approximation
234 * the given type T can represent, the machine epsilon of T.
235 * @tparam T a non integer float type
236 * @return machine epsilon of T
237 */
238 template<class T>
239 typename std::enable_if_t<std::is_floating_point_v<T>, T>
240 machineEpsilon() noexcept
241 {
242 const T one(1);
243 const T two(2);
244 T x = one, res;
245 do {
246 res = x;
247 } while (one + (x /= two) > one);
248 return res;
249 }
250
251 /** Returns true if the given value is less than epsilon, w/ epsilon > 0. */
252 template<class T>
253 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
254 constexpr is_zero(const T& a, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
255 return std::abs(a) < epsilon;
256 }
257
258 /** Returns true if all given values a and b are less than epsilon, w/ epsilon > 0. */
259 template<class T>
260 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
261 constexpr is_zero2f(const T& a, const T& b, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
262 return std::abs(a) < epsilon && std::abs(b) < epsilon;
263 }
264
265 /** Returns true if all given values a, b and c are less than epsilon, w/ epsilon > 0. */
266 template<class T>
267 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
268 constexpr is_zero3f(const T& a, const T& b, const T& c, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
269 return std::abs(a) < epsilon && std::abs(b) < epsilon && std::abs(c) < epsilon;
270 }
271
272 /** Returns true if all given values a, b, c and d are less than epsilon, w/ epsilon > 0. */
273 template<class T>
274 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
275 constexpr is_zero4f(const T& a, const T& b, const T& c, const T& d, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
276 return std::abs(a) < epsilon && std::abs(b) < epsilon && std::abs(c) < epsilon && std::abs(d) < epsilon;
277 }
278
279 /**
280 * Returns true if the given value is zero,
281 * disregarding `epsilon` but considering `NaN`, `-Inf` and `+Inf`.
282 */
283 template<class T>
284 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
285 constexpr is_zero_raw(const T& a) noexcept {
286 return ( bit_value(a) & ~float_iec559_sign_bit ) == 0;
287 }
288
289 /**
290 * Returns `-1`, `0` or `1` if `a` is less, equal or greater than `b`,
291 * disregarding epsilon but considering `NaN`, `-Inf` and `+Inf`.
292 *
293 * Implementation considers following corner cases:
294 * - NaN == NaN
295 * - +Inf == +Inf
296 * - -Inf == -Inf
297 * - NaN > 0
298 * - +Inf > -Inf
299 *
300 * @tparam T a non integer float type
301 * @param a value to compare
302 * @param b value to compare
303 */
304 template<class T,
305 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
306 constexpr int compare(const T a, const T b) noexcept {
307 if( a < b ) {
308 return -1; // Neither is NaN, a is smaller
309 }
310 if( a > b ) {
311 return 1; // Neither is NaN, a is larger
312 }
313 // a == b: we compare the _signed_ int value
314 typedef typename jau::uint_bytes_t<sizeof(T)> T_uint;
315 typedef typename std::make_signed_t<T_uint> T_int;
316 const T_int a_bits = static_cast<T_int>( bit_value(a) );
317 const T_int b_bits = static_cast<T_int>( bit_value(b) );
318 if( a_bits == b_bits ) {
319 return 0; // Values are equal (Inf, Nan .. )
320 } else if( a_bits < b_bits ) {
321 return -1; // (-0.0, 0.0) or (!NaN, NaN)
322 } else {
323 return 1; // ( 0.0, -0.0) or ( NaN, !NaN)
324 }
325 }
326
327 /**
328 * Returns `-1`, `0` or `1` if `a` is less, equal or greater than `b`,
329 * considering epsilon and `NaN`, `-Inf` and `+Inf`.
330 *
331 * `epsilon` must be > 0.
332 *
333 * Implementation considers following corner cases:
334 * - NaN == NaN
335 * - +Inf == +Inf
336 * - -Inf == -Inf
337 * - NaN > 0
338 * - +Inf > -Inf
339 *
340 * @tparam T a non integer float type
341 * @param a value to compare
342 * @param b value to compare
343 * @param epsilon defaults to std::numeric_limits<T>::epsilon(), must be > 0
344 */
345 template<class T,
346 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
347 constexpr int compare(const T a, const T b, const T epsilon) noexcept {
348 if( std::abs(a - b) < epsilon ) {
349 return 0;
350 } else {
351 return compare(a, b);
352 }
353 }
354
355 /**
356 * Returns true if both values are equal
357 * disregarding epsilon but considering `NaN`, `-Inf` and `+Inf`.
358 *
359 * Implementation considers following corner cases:
360 * - NaN == NaN
361 * - +Inf == +Inf
362 * - -Inf == -Inf
363 *
364 * @tparam T a non integer float type
365 * @param a value to compare
366 * @param b value to compare
367 */
368 template<class T>
369 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
370 constexpr equals_raw(const T& a, const T& b) noexcept {
371 // Values are equal (Inf, Nan .. )
372 return bit_value(a) == bit_value(b);
373 }
374
375 /**
376 * Returns true if both values are equal, i.e. their absolute delta < `epsilon`,
377 * considering epsilon and `NaN`, `-Inf` and `+Inf`.
378 *
379 * `epsilon` must be > 0.
380 *
381 * Implementation considers following corner cases:
382 * - NaN == NaN
383 * - +Inf == +Inf
384 * - -Inf == -Inf
385 *
386 * @tparam T a non integer float type
387 * @param a value to compare
388 * @param b value to compare
389 * @param epsilon defaults to std::numeric_limits<T>::epsilon(), must be > 0
390 */
391 template<class T>
392 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
393 constexpr equals(const T& a, const T& b, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
394 if( std::abs(a - b) < epsilon ) {
395 return true;
396 } else {
397 // Values are equal (Inf, Nan .. )
398 return bit_value(a) == bit_value(b);
399 }
400 }
401
402 /**
403 * Returns true if both values are equal, i.e. their absolute delta < `epsilon`,
404 * considering epsilon but disregarding `NaN`, `-Inf` and `+Inf`.
405 *
406 * @tparam T a non integer float type
407 * @param a value to compare
408 * @param b value to compare
409 */
410 template<class T>
411 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
412 constexpr equals2(const T& a, const T& b, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
413 return std::abs(a - b) < epsilon;
414 }
415
416 /**
417 * Returns true, if both floating point values are equal
418 * in the sense that their potential difference is less or equal <code>epsilon * ulp</code>.
419 *
420 * `epsilon` must be > 0.
421 *
422 * Implementation considers following corner cases:
423 * - NaN == NaN
424 * - +Inf == +Inf
425 * - -Inf == -Inf
426 *
427 * @tparam T a non integer float type
428 * @param a value to compare
429 * @param b value to compare
430 * @param ulp desired precision in ULPs (units in the last place)
431 * @param epsilon the machine epsilon of type T, defaults to <code>std::numeric_limits<T>::epsilon()</code>
432 */
433 template<class T>
434 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
435 constexpr equals(const T& a, const T& b, int ulp, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
436 return equals(a, b, epsilon * ulp);
437 }
438
439 /**
440 * Returns true, if both floating point values are equal
441 * in the sense that their potential difference is less or equal <code>epsilon * |a+b| * ulp</code>,
442 * where <code>|a+b|</code> scales epsilon to the magnitude of used values.
443 *
444 * `epsilon` must be > 0.
445 *
446 * Implementation considers following corner cases:
447 * - NaN == NaN
448 * - +Inf == +Inf
449 * - -Inf == -Inf
450 *
451 * @tparam T a non integer float type
452 * @param a value to compare
453 * @param b value to compare
454 * @param ulp desired precision in ULPs (units in the last place), defaults to 1
455 * @param epsilon the machine epsilon of type T, defaults to <code>std::numeric_limits<T>::epsilon()</code>
456 */
457 template<class T>
458 typename std::enable_if_t<std::is_floating_point_v<T>, bool>
459 almost_equal(const T& a, const T& b, int ulp=1, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept
460 {
461 const T diff = std::fabs(a-b);
462 if( ( diff <= epsilon * std::fabs(a+b) * ulp ) ||
463 ( diff < std::numeric_limits<T>::min() ) ) { // subnormal limit
464 return true;
465 } else {
466 // Values are equal (Inf, Nan .. )
467 return bit_value(a) == bit_value(b);
468 }
469 }
470
471 /** Returns the rounded value cast to int. */
472 template<class T,
473 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
474 constexpr typename jau::sint_bytes_t<sizeof(T)> round_to_int(const T v) noexcept {
475 return static_cast<typename jau::sint_bytes_t<sizeof(T)>>( std::round(v) );
476 }
477
478 /** Converts arc-degree to radians */
479 template<typename T,
480 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
481 constexpr T adeg_to_rad(const T arc_degree) noexcept {
482 return arc_degree * (T)M_PI / (T)180.0;
483 }
484
485 /** Converts radians to arc-degree */
486 template<typename T,
487 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
488 constexpr T rad_to_adeg(const T rad) noexcept {
489 return rad * (T)180.0 / (T)M_PI;
490 }
491
492 /**
493 * Appends a row of floating points to the given string `sb`
494 * @param sb string buffer to appends to
495 * @param f format string for each float element, e.g. "%10.5f"
496 * @param a the float data of size rows x columns
497 * @param rows float data `a` size row factor
498 * @param columns float data `a` size column factor
499 * @param rowMajorOrder if true floats are laid out in row-major-order, otherwise column-major-order (OpenGL)
500 * @param row selected row of float data `a`
501 * @return given string buffer `sb` for chaining
502 */
503 template<typename T,
504 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
505 std::string& row_to_string(std::string& sb, const std::string& f,
506 const T a[],
507 const jau::nsize_t rows, const jau::nsize_t columns,
508 const bool rowMajorOrder, const jau::nsize_t row) noexcept {
509 if(rowMajorOrder) {
510 for(jau::nsize_t c=0; c<columns; ++c) {
511 sb.append( jau::format_string(f.c_str(), a[ row*columns + c ] ) );
512 sb.append(", ");
513 }
514 } else {
515 for(jau::nsize_t c=0; c<columns; ++c) {
516 sb.append( jau::format_string(f.c_str(), a[ row + c*rows ] ) );
517 sb.append(", ");
518 }
519 }
520 return sb;
521 }
522
523 /**
524 * Appends a matrix of floating points to the given string `sb`
525 * @param sb string buffer to appends to
526 * @param rowPrefix prefix for each row
527 * @param f format string for each float element, e.g. "%10.5f"
528 * @param a the float data of size rows x columns
529 * @param rows float data `a` size row factor
530 * @param columns float data `a` size column factor
531 * @param rowMajorOrder if true floats are laid out in row-major-order, otherwise column-major-order (OpenGL)
532 * @return given string buffer `sb` for chaining
533 */
534 template<typename T,
535 std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
536 std::string& mat_to_string(std::string& sb, const std::string& rowPrefix, const std::string& f,
537 const T a[], const jau::nsize_t rows, const jau::nsize_t columns,
538 const bool rowMajorOrder) noexcept {
539 sb.append(rowPrefix).append("{\n");
540 for(jau::nsize_t i=0; i<rows; ++i) {
541 sb.append(rowPrefix).append(" ");
542 row_to_string(sb, f, a, rows, columns, rowMajorOrder, i);
543 sb.append("\n");
544 }
545 sb.append(rowPrefix).append("}").append("\n");
546 return sb;
547 }
548
549 /**@}*/
550
551} // namespace jau
552
553#endif /* JAU_FLOAT_MATH_HPP_ */
std::string & row_to_string(std::string &sb, const std::string &f, const T a[], const jau::nsize_t rows, const jau::nsize_t columns, const bool rowMajorOrder, const jau::nsize_t row) noexcept
Appends a row of floating points to the given string sb
std::enable_if_t< std::is_floating_point_v< T >, bool > almost_equal(const T &a, const T &b, int ulp=1, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true, if both floating point values are equal in the sense that their potential difference is...
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr equals(const T &a, const T &b, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if both values are equal, i.e.
constexpr uint32_t bit_value(const float a) noexcept
Returns the unsigned integer representation according to IEEE 754 (IEC 559) single floating-point bit...
constexpr uint32_t const float_iec559_positive_inf_bitval
Positive infinity bit-value of IEEE 754 (IEC 559) single float-point bit layout, i....
constexpr float float_value(const uint32_t a) noexcept
Converting IEEE 754 (IEC 559) single floating-point bit layout to float, see bit_value()
std::string & mat_to_string(std::string &sb, const std::string &rowPrefix, const std::string &f, const T a[], const jau::nsize_t rows, const jau::nsize_t columns, const bool rowMajorOrder) noexcept
Appends a matrix of floating points to the given string sb
constexpr uint64_t const double_iec559_exp_mask
Exponent mask bits 52-62 of IEEE 754 (IEC 559) double double-point bit layout, i.e.
constexpr uint32_t const float_iec559_mant_mask
Mantissa mask bits 0-22 of IEEE 754 (IEC 559) single float-point bit layout, i.e.
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr equals_raw(const T &a, const T &b) noexcept
Returns true if both values are equal disregarding epsilon but considering NaN, -Inf and +Inf.
constexpr uint32_t const float_iec559_exp_mask
Exponent mask bits 23-30 of IEEE 754 (IEC 559) single float-point bit layout, i.e.
std::enable_if_t< std::is_floating_point_v< T >, T > machineEpsilon() noexcept
Calculates the smallest floating point value approximation the given type T can represent,...
constexpr uint64_t const double_iec559_sign_bit
Signed bit 63 of IEEE 754 (IEC 559) double double-point bit layout, i.e.
constexpr uint32_t const float_iec559_nan_bitval
NaN bit-value of IEEE 754 (IEC 559) single float-point bit layout, i.e.
constexpr uint64_t const double_iec559_mant_mask
Mantissa mask bits 0-51 of IEEE 754 (IEC 559) double double-point bit layout, i.e.
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr is_zero4f(const T &a, const T &b, const T &c, const T &d, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if all given values a, b, c and d are less than epsilon, w/ epsilon > 0.
constexpr T rad_to_adeg(const T rad) noexcept
Converts radians to arc-degree.
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr is_zero(const T &a, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if the given value is less than epsilon, w/ epsilon > 0.
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
constexpr uint32_t const float_iec559_negative_inf_bitval
Negative infinity bit-value of IEEE 754 (IEC 559) single float-point bit layout, i....
jau::uint_bytes_t< sizeof(double)> double_uint_t
jau::uint_bytes_t< sizeof(float)> float_uint_t
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr is_zero3f(const T &a, const T &b, const T &c, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if all given values a, b and c are less than epsilon, w/ epsilon > 0.
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr equals2(const T &a, const T &b, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if both values are equal, i.e.
jau::uint_bytes_t< sizeof(T)> bit_value_raw(const T a) noexcept
Returns the unsigned integer representation according to IEEE 754 (IEC 559) floating-point bit layout...
constexpr uint32_t const float_iec559_sign_bit
Signed bit 31 of IEEE 754 (IEC 559) single float-point bit layout, i.e.
constexpr int compare(const T a, const T b) noexcept
Returns -1, 0 or 1 if a is less, equal or greater than b, disregarding epsilon but considering NaN,...
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr is_zero2f(const T &a, const T &b, const T &epsilon=std::numeric_limits< T >::epsilon()) noexcept
Returns true if all given values a and b are less than epsilon, w/ epsilon > 0.
constexpr jau::sint_bytes_t< sizeof(T)> round_to_int(const T v) noexcept
Returns the rounded value cast to int.
constexpr uint64_t const double_iec559_negative_inf_bitval
Negative infinity bit-value of IEEE 754 (IEC 559) double double-point bit layout, i....
constexpr double double_value(const uint64_t a) noexcept
Converting IEEE 754 (IEC 559) double floating-point bit layout to double, see bit_value()
constexpr uint64_t const double_iec559_nan_bitval
NaN bit-value of IEEE 754 (IEC 559) double double-point bit layout, i.e.
std::enable_if_t< std::is_floating_point_v< T >, bool > constexpr is_zero_raw(const T &a) noexcept
Returns true if the given value is zero, disregarding epsilon but considering NaN,...
constexpr uint64_t const double_iec559_positive_inf_bitval
Positive infinity bit-value of IEEE 754 (IEC 559) double double-point bit layout, i....
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition int_types.hpp:55
typename sint_bytes< bytesize >::type sint_bytes_t
Alias template for sint_bytes.
Definition int_types.hpp:89
typename uint_bytes< bytesize >::type uint_bytes_t
Alias template for uint_bytes.
Definition int_types.hpp:79
std::string format_string(const char *format,...)
Returns a string according to printf() formatting rules and variable number of arguments following th...
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32