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