43static const float NaN = std::numeric_limits<float>::quiet_NaN();
49static const double NaN_DBL = std::numeric_limits<double>::quiet_NaN();
78 std::enable_if_t<!std::numeric_limits<T>::is_integer,
bool> =
true>
85 std::enable_if_t<!std::numeric_limits<T>::is_integer,
bool> =
true>
88 REQUIRE( exp_a_bits == a_bits );
93TEST_CASE(
"Float IEEE 754 (IEC 559) Test 00",
"[math][datatype][float][iec559]" ) {
94 fprintf(stderr,
"float: ieee 754 / iec559: has %d\n",
95 std::numeric_limits<float>::is_iec559);
97 fprintf(stderr,
"float: +infinity: has %d, value 0x%X =?= 0x%X: %d\n",
98 std::numeric_limits<float>::has_infinity,
103 fprintf(stderr,
"float: -infinity: has %d, value 0x%X =?= 0x%X: %d\n",
104 std::numeric_limits<float>::has_infinity,
109 fprintf(stderr,
"float: quiet NAN: has %d, value 0x%X =?= 0x%X: %d\n",
110 std::numeric_limits<float>::has_quiet_NaN,
115 if( std::numeric_limits<float>::is_iec559 ) {
126TEST_CASE(
"Float Epsilon Test 01",
"[math][datatype][float][epsilon]" ) {
127 static float epsilon_f0 = std::numeric_limits<float>::epsilon();
128 static double epsilon_d0 = std::numeric_limits<double>::epsilon();
133 float epsilon_fd = epsilon_f1 - epsilon_f0;
134 double epsilon_dd = epsilon_d1 - epsilon_d0;
136 fprintf(stderr,
"std::numeric_limits<float>::epsilon() : %e\n", epsilon_f0);
137 fprintf(stderr,
"std::numeric_limits<double>::epsilon() : %le\n", epsilon_d0);
138 fprintf(stderr,
"jau::machineEpsilon<float>() : %e\n", epsilon_f1);
139 fprintf(stderr,
"jau::machineEpsilon<double>() : %le\n", epsilon_d1);
141 fprintf(stderr,
"float: approximation - numeric_limits : %e\n", epsilon_fd);
142 fprintf(stderr,
"double: approximation - numeric_limits : %le\n", epsilon_dd);
144 REQUIRE(
jau::equals(epsilon_f1, epsilon_f0, 5, epsilon_f0));
145 REQUIRE(
jau::equals(epsilon_d1, epsilon_d0, 5, epsilon_d0));
152static void testZeroWithEpsilon1(
const int tstNum,
const bool exp,
const float a,
const float epsilon=std::numeric_limits<float>::epsilon()) {
154 const float delta = a-0.0f;
155 fprintf(stderr,
"Zero.WE.%d: a: %f, -> d %f, exp %d, zero %d, epsilon %f\n",
156 tstNum, a, delta, exp,
zero, epsilon);
157 REQUIRE(exp ==
zero);
171TEST_CASE(
"Float Zero Fixed Epsilon Test 10",
"[math][datatype][float][epsilon]" ) {
174TEST_CASE(
"Float Zero Mach Epsilon Test 11",
"[math][datatype][float][epsilon]" ) {
180 const float delta = a-0.0f;
181 fprintf(stderr,
"Zero.NE.%d: a: %f, -> d %f, exp %d, zero %d\n",
182 tstNum, a, delta, exp,
zero);
183 REQUIRE(exp ==
zero);
185TEST_CASE(
"Float Zero No Epsilon Test 12",
"[math][datatype][float][epsilon]" ) {
201static void testEqualsWithEpsilon(
const int tstNum,
const bool exp,
const float a,
const float b,
const float epsilon=std::numeric_limits<float>::epsilon()) {
204 const bool comp_eq = 0 == comp;
205 const float delta = a-b;
206 fprintf(stderr,
"Equal.WE.%d: a: %f, b: %f -> d %f, exp %d, equal %d, comp %d, epsilon %f\n",
207 tstNum, a, b, delta, exp, equal, comp, epsilon);
208 REQUIRE(exp == comp_eq);
209 REQUIRE(exp == equal);
230TEST_CASE(
"Float Equals Fixed Epsilon Test 20",
"[math][datatype][float][epsilon]" ) {
234TEST_CASE(
"Float Equals Mach Epsilon Test 21",
"[math][datatype][float][epsilon]" ) {
241 const bool comp_eq = 0 == comp;
242 const float delta = a-b;
243 fprintf(stderr,
"Equal.NE.%d: a: %f, b: %f -> d %f, exp %d, equal %d, comp %d\n",
244 tstNum, a, b, delta, exp, equal, comp);
245 REQUIRE(exp == comp_eq);
246 REQUIRE(exp == equal);
249TEST_CASE(
"Float Equals No Epsilon Test 22",
"[math][datatype][float][epsilon]" ) {
270 const float delta = a-b;
273 const int32_t a_sbits =
static_cast<int32_t
>(a_bits);
274 const int32_t b_sbits =
static_cast<int32_t
>(b_bits);
275 fprintf(stderr,
"Comp.NE.%d: a: %f 0x%X %d, b: %f 0x%X %d -> d %f, equal %d, comp: exp %d, has %d\n",
276 tstNum, a, a_bits, a_sbits, b, b_bits, b_sbits, delta, equal, exp, comp);
277 REQUIRE(exp == comp);
280TEST_CASE(
"Float Compare Zero Epsilon Test 10",
"[math][datatype][float][epsilon]" ) {
297 if( std::numeric_limits<float>::has_infinity ) {
304 if( std::numeric_limits<float>::has_quiet_NaN ) {
314 const float delta = a-b;
315 fprintf(stderr,
"Comp.WE.%d: a: %f, b: %f -> d %f, equal %d, comp: exp %d, has %d\n",
316 tstNum, a, b, delta, equal, exp, comp);
317 REQUIRE(exp == comp);
340 if( std::numeric_limits<float>::has_infinity ) {
347 if( std::numeric_limits<float>::has_quiet_NaN ) {
354TEST_CASE(
"Float Compare Fixed Epsilon Test 20",
"[math][datatype][float][epsilon]" ) {
358TEST_CASE(
"Float Compare Mac Epsilon Test 21",
"[math][datatype][float][epsilon]" ) {
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()
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_nan_bitval
NaN bit-value of IEEE 754 (IEC 559) single float-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 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_negative_inf_bitval
Negative infinity bit-value of IEEE 754 (IEC 559) single float-point bit layout, i....
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 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 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)
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
constexpr const jau::fraction_i64 zero(0l, 1lu)
zero is 0/1
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
static void testZeroWithEpsilon1(const int tstNum, const bool exp, const float a, const float epsilon=std::numeric_limits< float >::epsilon())
void testIEC559DoubleType()
static void testZeroNoEpsilon(const int tstNum, const bool exp, const float a)
void testFloatBits(const T a)
static const double NEGATIVE_INFINITY_DBL
static const double NaN_DBL
static const double POSITIVE_INFINITY_DBL
static void testCompareNoEpsilon(const int tstNum, const int exp, const float a, const float b)
static void testEqualsNoEpsilon(const int tstNum, const bool exp, const float a, const float b)
static const float MAX_VALUE
void testIEC559FloatType()
static const float MACH_EPSILON
static const float POSITIVE_INFINITY
static void testCompareWithEpsilon(const int tstNum, const int exp, const float a, const float b, const float epsilon)
static void testEqualsWithEpsilon(const int tstNum, const bool exp, const float a, const float b, const float epsilon=std::numeric_limits< float >::epsilon())
static void test05CompareWithEpsilon(int i, const float epsilon)
static const float MIN_VALUE
static const float NEGATIVE_INFINITY
static const double MACH_EPSILON_DOUBLE
TEST_CASE("Float IEEE 754 (IEC 559) Test 00", "[math][datatype][float][iec559]")