25#ifndef JAU_FLOAT_MATH_HPP_
26#define JAU_FLOAT_MATH_HPP_
51 using namespace jau::int_literals;
126 template<std::
floating_po
int T>
131 union { T_uint u; T
f; } iec559 = { .f = a };
144 union { uint32_t u;
float f; } iec559 = { .f = a };
174 if( std::isnan(a) ) {
182 union { uint32_t u;
float f; } iec559 = { .u = a };
194 union { uint64_t u;
double f; } iec559 = { .f = a };
221 if( std::isnan(a) ) {
228 union { uint64_t u;
double f; } iec559 = { .u = a };
238 template<std::
floating_po
int T>
251 template<std::
floating_po
int T>
252 constexpr bool is_zero(
const T& a,
const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept {
253 return std::abs(a) < epsilon;
257 template<std::
floating_po
int 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;
263 template<std::
floating_po
int 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;
269 template<std::
floating_po
int 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;
278 template<std::
floating_po
int T>
298 template<std::
floating_po
int T>
299 constexpr int compare(
const T a,
const T b)
noexcept {
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 ) {
313 }
else if( a_bits < b_bits ) {
338 template<std::
floating_po
int T>
339 constexpr int compare(
const T a,
const T b,
const T epsilon)
noexcept {
340 if( std::abs(a - b) < epsilon ) {
360 template<std::
floating_po
int T>
382 template<std::
floating_po
int 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 ) {
400 template<std::
floating_po
int 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;
422 template<std::
floating_po
int 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);
445 template<std::
floating_po
int T>
446 constexpr bool almost_equal(
const T& a,
const T& b,
int ulp=1,
const T& epsilon=std::numeric_limits<T>::epsilon()) noexcept
448 const T diff = std::fabs(a-b);
449 if( ( diff <= epsilon * std::fabs(a+b) * ulp ) ||
450 ( diff < std::numeric_limits<T>::min() ) ) {
459 template<std::
floating_po
int T>
465 template<std::
floating_po
int T>
471 template<std::
floating_po
int T>
473 return arc_degree * (T)M_PI / (T)180.0;
477 template<std::
floating_po
int T>
479 return rad * (T)180.0 / (T)M_PI;
493 template<std::
floating_po
int T>
497 const bool rowMajorOrder,
const jau::nsize_t row)
noexcept {
523 template<std::
floating_po
int T>
524 std::string&
mat_to_string(std::string& sb,
const std::string& rowPrefix,
const std::string_view
f,
526 const bool rowMajorOrder)
noexcept {
527 sb.append(rowPrefix).append(
"{\n");
529 sb.append(rowPrefix).append(
" ");
533 sb.append(rowPrefix).append(
"}").append(
"\n");
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.
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...
typename uint_bytes< bytesize >::type uint_bytes_t
Alias template for uint_bytes.
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.
static std::string f(uint32_t v)