Bonita Montero
2024-11-24 16:29:00 UTC
This code combines finite values with NaN-values and Inf-values and
compares them with all basic comparison operators.
#include <iostream>
#include <limits>
using namespace std;
int main()
{
constexpr double
NaN = numeric_limits<double>::quiet_NaN(),
Inf = numeric_limits<double>::infinity(),
Finite = 1.0;
static struct value_t
{
char const *what;
double value;
} const values[] =
{
{ "Finite", Finite },
{ "Inf", Inf },
{ "NaN", NaN }
};
static struct op_t
{
char const *what;
bool (*op)( double, double );
} const ops[] =
{
{ "<", +[]( double a, double b ) { return a < b; } },
{ "<=", +[]( double a, double b ) { return a <= b; } },
{ "==", +[]( double a, double b ) { return a == b; } },
{ ">=", +[]( double a, double b ) { return a >= b; } },
{ ">", +[]( double a, double b ) { return a > b; } }
};
for( value_t const &vA : values )
{
cout << vA.what << endl;
for( value_t const &vB : values )
{
if( &vA == &vB && &vA == values )
continue;
cout << "\t" << vB.what << endl;
for( op_t const &op : ops )
{
cout << "\t\t" << op.what << ": ";
cout << (ops->op( vA.value, vB.value ) ? "true" : "false") << endl;
}
}
}
}
This are the results:
Finite
Inf
<: true
<=: true
==: true
<: false
<=: false
==: false
Finite
<: false
<=: false
==: false
<: false
<=: false
==: false
<: false
<=: false
==: false
Finite
<: false
<=: false
==: false
<: false
<=: false
==: false
<: false
<=: false
==: false
compares them with all basic comparison operators.
#include <iostream>
#include <limits>
using namespace std;
int main()
{
constexpr double
NaN = numeric_limits<double>::quiet_NaN(),
Inf = numeric_limits<double>::infinity(),
Finite = 1.0;
static struct value_t
{
char const *what;
double value;
} const values[] =
{
{ "Finite", Finite },
{ "Inf", Inf },
{ "NaN", NaN }
};
static struct op_t
{
char const *what;
bool (*op)( double, double );
} const ops[] =
{
{ "<", +[]( double a, double b ) { return a < b; } },
{ "<=", +[]( double a, double b ) { return a <= b; } },
{ "==", +[]( double a, double b ) { return a == b; } },
{ ">=", +[]( double a, double b ) { return a >= b; } },
{ ">", +[]( double a, double b ) { return a > b; } }
};
for( value_t const &vA : values )
{
cout << vA.what << endl;
for( value_t const &vB : values )
{
if( &vA == &vB && &vA == values )
continue;
cout << "\t" << vB.what << endl;
for( op_t const &op : ops )
{
cout << "\t\t" << op.what << ": ";
cout << (ops->op( vA.value, vB.value ) ? "true" : "false") << endl;
}
}
}
}
This are the results:
Finite
Inf
<: true
<=: true
==: true
=: true
: true
NaN: true
<: false
<=: false
==: false
=: false
: false
Inf: false
Finite
<: false
<=: false
==: false
=: false
: false
Inf: false
<: false
<=: false
==: false
=: false
: false
NaN: false
<: false
<=: false
==: false
=: false
: false
NaN: false
Finite
<: false
<=: false
==: false
=: false
: false
Inf: false
<: false
<=: false
==: false
=: false
: false
NaN: false
<: false
<=: false
==: false
=: false
: false
Interesting results !: false