jaulib v1.4.1
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 <concepts>
31#include <type_traits>
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_t<sizeof(float)> float_uint_t;
54 typedef typename jau::uint_bytes_t<sizeof(double)> 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<std::floating_point T>
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<std::floating_point T>
239 T machineEpsilon() noexcept
240 {
241 const T one(1);
242 const T two(2);
243 T x = one, res;
244 do {
245 res = x;
246 } while (one + (x /= two) > one);
247 return res;
248 }
249
250 /** Returns true if the given value is less than epsilon, w/ epsilon > 0. */
251 template<std::floating_point T>
252 constexpr bool is_zero(const T& a, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
253 return std::abs(a) < epsilon;
254 }
255
256 /** Returns true if all given values a and b are less than epsilon, w/ epsilon > 0. */
257 template<std::floating_point T>
258 constexpr bool is_zero2f(const T& a, const T& b, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
259 return std::abs(a) < epsilon && std::abs(b) < epsilon;
260 }
261
262 /** Returns true if all given values a, b and c are less than epsilon, w/ epsilon > 0. */
263 template<std::floating_point T>
264 constexpr bool is_zero3f(const T& a, const T& b, const T& c, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
265 return std::abs(a) < epsilon && std::abs(b) < epsilon && std::abs(c) < epsilon;
266 }
267
268 /** Returns true if all given values a, b, c and d are less than epsilon, w/ epsilon > 0. */
269 template<std::floating_point T>
270 constexpr bool is_zero4f(const T& a, const T& b, const T& c, const T& d, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
271 return std::abs(a) < epsilon && std::abs(b) < epsilon && std::abs(c) < epsilon && std::abs(d) < epsilon;
272 }
273
274 /**
275 * Returns true if the given value is zero,
276 * disregarding `epsilon` but considering `NaN`, `-Inf` and `+Inf`.
277 */
278 template<std::floating_point T>
279 constexpr bool is_zero_raw(const T& a) noexcept {
280 return ( bit_value(a) & ~float_iec559_sign_bit ) == 0;
281 }
282
283 /**
284 * Returns `-1`, `0` or `1` if `a` is less, equal or greater than `b`,
285 * disregarding epsilon but considering `NaN`, `-Inf` and `+Inf`.
286 *
287 * Implementation considers following corner cases:
288 * - NaN == NaN
289 * - +Inf == +Inf
290 * - -Inf == -Inf
291 * - NaN > 0
292 * - +Inf > -Inf
293 *
294 * @tparam T a non integer float type
295 * @param a value to compare
296 * @param b value to compare
297 */
298 template<std::floating_point T>
299 constexpr int compare(const T a, const T b) noexcept {
300 if( a < b ) {
301 return -1; // Neither is NaN, a is smaller
302 }
303 if( a > b ) {
304 return 1; // Neither is NaN, a is larger
305 }
306 // a == b: we compare the _signed_ int value
307 typedef typename jau::uint_bytes_t<sizeof(T)> T_uint;
308 typedef typename std::make_signed_t<T_uint> T_int;
309 const T_int a_bits = static_cast<T_int>( bit_value(a) );
310 const T_int b_bits = static_cast<T_int>( bit_value(b) );
311 if( a_bits == b_bits ) {
312 return 0; // Values are equal (Inf, Nan .. )
313 } else if( a_bits < b_bits ) {
314 return -1; // (-0.0, 0.0) or (!NaN, NaN)
315 } else {
316 return 1; // ( 0.0, -0.0) or ( NaN, !NaN)
317 }
318 }
319
320 /**
321 * Returns `-1`, `0` or `1` if `a` is less, equal or greater than `b`,
322 * considering epsilon and `NaN`, `-Inf` and `+Inf`.
323 *
324 * `epsilon` must be > 0.
325 *
326 * Implementation considers following corner cases:
327 * - NaN == NaN
328 * - +Inf == +Inf
329 * - -Inf == -Inf
330 * - NaN > 0
331 * - +Inf > -Inf
332 *
333 * @tparam T a non integer float type
334 * @param a value to compare
335 * @param b value to compare
336 * @param epsilon defaults to std::numeric_limits<T>::epsilon(), must be > 0
337 */
338 template<std::floating_point T>
339 constexpr int compare(const T a, const T b, const T epsilon) noexcept {
340 if( std::abs(a - b) < epsilon ) {
341 return 0;
342 } else {
343 return compare(a, b);
344 }
345 }
346
347 /**
348 * Returns true if both values are equal
349 * disregarding epsilon but considering `NaN`, `-Inf` and `+Inf`.
350 *
351 * Implementation considers following corner cases:
352 * - NaN == NaN
353 * - +Inf == +Inf
354 * - -Inf == -Inf
355 *
356 * @tparam T a non integer float type
357 * @param a value to compare
358 * @param b value to compare
359 */
360 template<std::floating_point T>
361 constexpr bool equals_raw(const T& a, const T& b) noexcept {
362 // Values are equal (Inf, Nan .. )
363 return bit_value(a) == bit_value(b);
364 }
365
366 /**
367 * Returns true if both values are equal, i.e. their absolute delta < `epsilon`,
368 * considering epsilon and `NaN`, `-Inf` and `+Inf`.
369 *
370 * `epsilon` must be > 0.
371 *
372 * Implementation considers following corner cases:
373 * - NaN == NaN
374 * - +Inf == +Inf
375 * - -Inf == -Inf
376 *
377 * @tparam T a non integer float type
378 * @param a value to compare
379 * @param b value to compare
380 * @param epsilon defaults to std::numeric_limits<T>::epsilon(), must be > 0
381 */
382 template<std::floating_point T>
383 constexpr bool equals(const T& a, const T& b, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
384 if( std::abs(a - b) < epsilon ) {
385 return true;
386 } else {
387 // Values are equal (Inf, Nan .. )
388 return bit_value(a) == bit_value(b);
389 }
390 }
391
392 /**
393 * Returns true if both values are equal, i.e. their absolute delta < `epsilon`,
394 * considering epsilon but disregarding `NaN`, `-Inf` and `+Inf`.
395 *
396 * @tparam T a non integer float type
397 * @param a value to compare
398 * @param b value to compare
399 */
400 template<std::floating_point T>
401 constexpr bool equals2(const T& a, const T& b, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
402 return std::abs(a - b) < epsilon;
403 }
404
405 /**
406 * Returns true, if both floating point values are equal
407 * in the sense that their potential difference is less or equal <code>epsilon * ulp</code>.
408 *
409 * `epsilon` must be > 0.
410 *
411 * Implementation considers following corner cases:
412 * - NaN == NaN
413 * - +Inf == +Inf
414 * - -Inf == -Inf
415 *
416 * @tparam T a non integer float type
417 * @param a value to compare
418 * @param b value to compare
419 * @param ulp desired precision in ULPs (units in the last place)
420 * @param epsilon the machine epsilon of type T, defaults to <code>std::numeric_limits<T>::epsilon()</code>
421 */
422 template<std::floating_point T>
423 constexpr bool equals(const T& a, const T& b, int ulp, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
424 return equals(a, b, epsilon * ulp);
425 }
426
427 /**
428 * Returns true, if both floating point values are equal
429 * in the sense that their potential difference is less or equal <code>epsilon * |a+b| * ulp</code>,
430 * where <code>|a+b|</code> scales epsilon to the magnitude of used values.
431 *
432 * `epsilon` must be > 0.
433 *
434 * Implementation considers following corner cases:
435 * - NaN == NaN
436 * - +Inf == +Inf
437 * - -Inf == -Inf
438 *
439 * @tparam T a non integer float type
440 * @param a value to compare
441 * @param b value to compare
442 * @param ulp desired precision in ULPs (units in the last place), defaults to 1
443 * @param epsilon the machine epsilon of type T, defaults to <code>std::numeric_limits<T>::epsilon()</code>
444 */
445 template<std::floating_point T>
446 constexpr bool almost_equal(const T& a, const T& b, int ulp=1, const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept
447 {
448 const T diff = std::fabs(a-b);
449 if( ( diff <= epsilon * std::fabs(a+b) * ulp ) ||
450 ( diff < std::numeric_limits<T>::min() ) ) { // subnormal limit
451 return true;
452 } else {
453 // Values are equal (Inf, Nan .. )
454 return bit_value(a) == bit_value(b);
455 }
456 }
457
458 /** Returns the rounded value cast to signed int. */
459 template<std::floating_point T>
460 constexpr typename jau::sint_bytes_t<sizeof(T)> round_to_int(const T v) noexcept {
461 return static_cast<typename jau::sint_bytes_t<sizeof(T)>>( std::round(v) );
462 }
463
464 /** Returns the rounded value cast to unsigned int. */
465 template<std::floating_point T>
466 constexpr typename jau::uint_bytes_t<sizeof(T)> round_to_uint(const T v) noexcept {
467 return static_cast<typename jau::uint_bytes_t<sizeof(T)>>( std::round(v) );
468 }
469
470 /** Converts arc-degree to radians */
471 template<std::floating_point T>
472 constexpr T adeg_to_rad(const T arc_degree) noexcept {
473 return arc_degree * (T)M_PI / (T)180.0;
474 }
475
476 /** Converts radians to arc-degree */
477 template<std::floating_point T>
478 constexpr T rad_to_adeg(const T rad) noexcept {
479 return rad * (T)180.0 / (T)M_PI;
480 }
481
482 /**
483 * Appends a row of floating points to the given string `sb`
484 * @param sb string buffer to appends to
485 * @param f format string for each float element, e.g. "%10.5f"
486 * @param a the float data of size rows x columns
487 * @param rows float data `a` size row factor
488 * @param columns float data `a` size column factor
489 * @param rowMajorOrder if true floats are laid out in row-major-order, otherwise column-major-order (OpenGL)
490 * @param row selected row of float data `a`
491 * @return given string buffer `sb` for chaining
492 */
493 template<std::floating_point T>
494 std::string& row_to_string(std::string& sb, const std::string_view f,
495 const T a[],
496 const jau::nsize_t rows, const jau::nsize_t columns,
497 const bool rowMajorOrder, const jau::nsize_t row) noexcept {
498 if(rowMajorOrder) {
499 for(jau::nsize_t c=0; c<columns; ++c) {
500 sb.append( jau::format_string(f, a[ row*columns + c ] ) );
501 sb.append(", ");
502 }
503 } else {
504 for(jau::nsize_t c=0; c<columns; ++c) {
505 sb.append( jau::format_string(f, a[ row + c*rows ] ) );
506 sb.append(", ");
507 }
508 }
509 return sb;
510 }
511
512 /**
513 * Appends a matrix of floating points to the given string `sb`
514 * @param sb string buffer to appends to
515 * @param rowPrefix prefix for each row
516 * @param f format string for each float element, e.g. "%10.5f"
517 * @param a the float data of size rows x columns
518 * @param rows float data `a` size row factor
519 * @param columns float data `a` size column factor
520 * @param rowMajorOrder if true floats are laid out in row-major-order, otherwise column-major-order (OpenGL)
521 * @return given string buffer `sb` for chaining
522 */
523 template<std::floating_point T>
524 std::string& mat_to_string(std::string& sb, const std::string& rowPrefix, const std::string_view f,
525 const T a[], const jau::nsize_t rows, const jau::nsize_t columns,
526 const bool rowMajorOrder) noexcept {
527 sb.append(rowPrefix).append("{\n");
528 for(jau::nsize_t i=0; i<rows; ++i) {
529 sb.append(rowPrefix).append(" ");
530 row_to_string(sb, f, a, rows, columns, rowMajorOrder, i);
531 sb.append("\n");
532 }
533 sb.append(rowPrefix).append("}").append("\n");
534 return sb;
535 }
536
537 /**@}*/
538
539} // namespace jau
540
541#endif /* JAU_FLOAT_MATH_HPP_ */
constexpr bool 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.
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,...
constexpr T rad_to_adeg(const T rad) noexcept
Converts radians to arc-degree.
constexpr 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...
constexpr jau::sint_bytes_t< sizeof(T)> round_to_int(const T v) noexcept
Returns the rounded value cast to signed int.
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()
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 bool is_zero_raw(const T &a) noexcept
Returns true if the given value is zero, disregarding epsilon but considering NaN,...
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.
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.
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.
constexpr bool 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 bool 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 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
constexpr bool 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 bool 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.
constexpr T adeg_to_rad(const T arc_degree) noexcept
Converts arc-degree to radians.
std::string & row_to_string(std::string &sb, const std::string_view 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
constexpr uint32_t const float_iec559_sign_bit
Signed bit 31 of IEEE 754 (IEC 559) single float-point bit layout, i.e.
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 bool 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 uint64_t const double_iec559_nan_bitval
NaN bit-value of IEEE 754 (IEC 559) double double-point bit layout, i.e.
std::string & mat_to_string(std::string &sb, const std::string &rowPrefix, const std::string_view 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
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...
T machineEpsilon() noexcept
Calculates the smallest floating point value approximation the given type T can represent,...
constexpr jau::uint_bytes_t< sizeof(T)> round_to_uint(const T v) noexcept
Returns the rounded value cast to unsigned int.
constexpr bool 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 uint64_t const double_iec559_positive_inf_bitval
Positive infinity bit-value of IEEE 754 (IEC 559) double double-point bit layout, i....
typename sint_bytes< bytesize >::type sint_bytes_t
Alias template for sint_bytes.
Definition int_types.hpp:65
uint_bytes_t< sizeof(unsigned long int)> nsize_t
Natural 'size_t' alternative using uint<XX>_t with xx = sizeof(unsigned long int)*8 as its natural si...
Definition int_types.hpp:85
typename uint_bytes< bytesize >::type uint_bytes_t
Alias template for uint_bytes.
Definition int_types.hpp:55
constexpr std::string format_string(const std::string_view format, const Args &...args)
Safely returns a (non-truncated) string according to snprintf() formatting rules using an initially s...
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
static std::string f(uint32_t v)