25#ifndef JAU_FLOAT_MATH_HPP_
26#define JAU_FLOAT_MATH_HPP_
127 std::enable_if_t<std::is_floating_point_v<T>,
bool> =
true>
132 union { T_uint u; T f; } iec559 = { .f = a };
145 union { uint32_t u;
float f; } iec559 = { .f = a };
175 if( std::isnan(a) ) {
183 union { uint32_t u;
float f; } iec559 = { .u = a };
195 union { uint64_t u;
double f; } iec559 = { .f = a };
222 if( std::isnan(a) ) {
229 union { uint64_t u;
double f; } iec559 = { .u = a };
240 typename std::enable_if<std::is_floating_point_v<T>, T>::type
248 }
while (
one + (x /= two) >
one);
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 {
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 {
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 {
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 {
285 typename std::enable_if<std::is_floating_point_v<T>,
bool>::type
306 std::enable_if_t<std::is_floating_point_v<T>,
bool> =
true>
307 constexpr int compare(
const T a,
const T b)
noexcept {
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 ) {
321 }
else if( a_bits < b_bits ) {
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 {
370 typename std::enable_if<std::is_floating_point_v<T>,
bool>::type
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 {
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 {
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);
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
462 const T diff = std::fabs(a-b);
463 if( ( diff <= epsilon * std::fabs(a+b) * ulp ) ||
474 std::enable_if_t<std::is_floating_point_v<T>,
bool> =
true>
476 return static_cast<typename
jau::sint_bytes<sizeof(T)
>::type>( std::round(v) );
481 std::enable_if_t<std::is_floating_point_v<T>,
bool> =
true>
483 return arc_degree * (T)M_PI / (T)180.0;
488 std::enable_if_t<std::is_floating_point_v<T>,
bool> =
true>
490 return rad * (T)180.0 / (T)M_PI;
505 std::enable_if_t<std::is_floating_point_v<T>,
bool> =
true>
509 const bool rowMajorOrder,
const jau::nsize_t row)
noexcept {
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,
539 const bool rowMajorOrder)
noexcept {
540 sb.append(rowPrefix).append(
"{\n");
542 sb.append(rowPrefix).append(
" ");
546 sb.append(rowPrefix).append(
"}").append(
"\n");
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
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....
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,...
constexpr float float_value(const uint32_t a) noexcept
Converting IEEE 754 (IEC 559) single floating-point bit layout to float, see bit_value()
jau::uint_bytes< sizeof(float)>::type float_uint_t
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.
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< 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.
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< std::is_floating_point_v< T >, T >::type 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.
jau::uint_bytes< sizeof(double)>::type double_uint_t
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< 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.
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.
constexpr T rad_to_adeg(const T rad) noexcept
Converts radians to arc-degree.
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.
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...
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.
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....
constexpr jau::sint_bytes< sizeof(T)>::type round_to_int(const T v) noexcept
Returns the rounded value cast to int.
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...
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< 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.
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.
constexpr uint64_t const double_iec559_positive_inf_bitval
Positive infinity bit-value of IEEE 754 (IEC 559) double double-point bit layout, i....
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
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.