Discussion:
Interesting results
(too old to reply)
Bonita Montero
2024-11-24 16:29:00 UTC
Permalink
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
=: true
: true
NaN
<: false
<=: false
==: false
=: false
: false
Inf
Finite
<: false
<=: false
==: false
=: false
: false
Inf
<: false
<=: false
==: false
=: false
: false
NaN
<: false
<=: false
==: false
=: false
: false
NaN
Finite
<: false
<=: false
==: false
=: false
: false
Inf
<: false
<=: false
==: false
=: false
: false
NaN
<: false
<=: false
==: false
=: false
: false
Interesting results !
Paavo Helde
2024-11-24 22:25:00 UTC
Permalink
Post by Bonita Montero
#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 << ": ";
"false") << endl;
Change the above line to actually use the loop iterator:

cout << (op.op(vA.value, vB.value) ? "true" : "false") << endl;

and the results will become much more boring.
Post by Bonita Montero
            }
        }
    }
}
Bonita Montero
2024-11-25 05:32:44 UTC
Permalink
       cout << (op.op(vA.value, vB.value) ? "true" : "false") << endl;
and the results will become much more boring.
Yes, I accidentally used the <-operator only.
Michael S
2024-11-24 23:04:52 UTC
Permalink
On Sun, 24 Nov 2024 17:29:00 +0100
Post by Bonita Montero
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; }
}
}
}
Finite
Inf
<: true
<=: true
==: true
Post by Bonita Montero
=: true
: true
NaN
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Inf
Finite
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Inf
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
NaN
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
NaN
Finite
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Inf
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
NaN
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Interesting results !
Nothing of interest here, except for another proof that combination of
borderline-sane programming language with insane programming style
causes bugs.
Here is the same test written in more sane language and style. Results
are 100% as expected:

#include <stdio.h>
#include <math.h>

int main()
{
static const double vals[3] = { 1.0, INFINITY, NAN };
_Bool results[3][3][5];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
results[i][j][0] = vals[i] < vals[j];
results[i][j][1] = vals[i] <= vals[j];
results[i][j][2] = vals[i] == vals[j];
results[i][j][3] = vals[i] >= vals[j];
results[i][j][4] = vals[i] > vals[j];
}
}

static const char* ops[5] = { "<", "<=", "==", ">=", ">" };
for (int i = 0; i < 3; ++i) {
for (int k = 0; k < 5; ++k) {
for (int j = 0; j < 3; ++j)
printf(" %3g %2s %3g : %-5s ;",
vals[i], ops[k], vals[j],
results[i][j][k] ? "true" : "false");
printf("\n");
}
}
return 0;
}

Results:
1 < 1 : false ; 1 < inf : true ; 1 < nan : false ;
1 <= 1 : true ; 1 <= inf : true ; 1 <= nan : false ;
1 == 1 : true ; 1 == inf : false ; 1 == nan : false ;
1 >= 1 : true ; 1 >= inf : false ; 1 >= nan : false ;
1 > 1 : false ; 1 > inf : false ; 1 > nan : false ;
inf < 1 : false ; inf < inf : false ; inf < nan : false ;
inf <= 1 : false ; inf <= inf : true ; inf <= nan : false ;
inf == 1 : false ; inf == inf : true ; inf == nan : false ;
inf >= 1 : true ; inf >= inf : true ; inf >= nan : false ;
inf > 1 : true ; inf > inf : false ; inf > nan : false ;
nan < 1 : false ; nan < inf : false ; nan < nan : false ;
nan <= 1 : false ; nan <= inf : false ; nan <= nan : false ;
nan == 1 : false ; nan == inf : false ; nan == nan : false ;
nan >= 1 : false ; nan >= inf : false ; nan >= nan : false ;
nan > 1 : false ; nan > inf : false ; nan > nan : false ;
Bonita Montero
2024-11-25 05:32:00 UTC
Permalink
Post by Michael S
Nothing of interest here, except for another proof that combination of
borderline-sane programming language with insane programming style
causes bugs.
There's nothing insane with my coding; I just like functional programming.
Post by Michael S
Here is the same test written in more sane language and style. Results
Your code is less readable for me.
wij
2024-11-25 19:32:09 UTC
Permalink
Post by Bonita Montero
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;
}
}
}
}
Finite
         Inf
                 <: true
                 <=: true
                 ==: true
                 >=: true
                 >: true
         NaN
                 <: false
                 <=: false
                 ==: false
                 >=: false
                 >: false
Inf
         Finite
                 <: false
                 <=: false
                 ==: false
                 >=: false
                 >: false
         Inf
                 <: false
                 <=: false
                 ==: false
                 >=: false
                 >: false
         NaN
                 <: false
                 <=: false
                 ==: false
                 >=: false
                 >: false
NaN
         Finite
                 <: false
                 <=: false
                 ==: false
                 >=: false
                 >: false
         Inf
                 <: false
                 <=: false
                 ==: false
                 >=: false
                 >: false
         NaN
                 <: false
                 <=: false
                 ==: false
                 >=: false
                 >: false
Interesting results !
#include <Wy.stdio.h>
#include <Wy.math.h>

using namespace Wy;

typedef double FType;
bool lt(FType a, FType b) { return a<b; };
bool le(FType a, FType b) { return a<=b; };
bool eq(FType a, FType b) { return a==b; };
bool ge(FType a, FType b) { return a>=b; };
bool gt(FType a, FType b) { return a>b; };

int main(int argc, const char* argv[])
try {
constexpr FType
NaN= nan<FType>(""),
Inf= infinity<FType>(),
Finite=1.0;

struct value_t {
const char* what;
double value;
} const values[]= {
{"Finite",Finite},
{"Inf",Inf},
{"Nan",NaN}
};

struct op_t {
const char* what;
bool (*op)(FType,FType);
} const ops[]={
{"<",lt},
{"<=",le},
{"==",eq},
{">=",ge},
{">",gt},
};

for(size_t i=0; i<WY_CARRLEN(values); ++i) {
const value_t& vA= values[i];
cout << vA.what << WY_ENDL;
for(size_t j=0; j<WY_CARRLEN(values); ++j) {
const value_t& vB= values[j];
if((&vA==&vB)&&(&vA==values)) {
continue;
}
cout << "\t" << vB.what << WY_ENDL;
for(size_t k=0; k<WY_CARRLEN(ops); ++k) {
const op_t& op= ops[k];
cout << "\t\t" << op.what << ": ";
cout << (ops->op( vA.value, vB.value ) ? "true" : "false") << WY_ENDL;
}
}
}

cout << "OK" WY_ENDL;
return 0;
}
catch(const Errno& e) {
cerr << wrd(e) << WY_ENDL;
return e.c_errno();
}
catch(...) {
cerr << "main() caught(...)" WY_ENDL;
throw;
};

--------------------
This are the results:

Finite
Inf
<: true
<=: true
==: true
Post by Bonita Montero
=: true
: true
Nan
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Inf
Finite
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Inf
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Nan
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Nan
Finite
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Inf
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
Nan
<: false
<=: false
==: false
Post by Bonita Montero
=: false
: false
OK
wij
2024-11-25 19:41:07 UTC
Permalink
This is what is interesting: (you can't do what the following does)

---------------------------
#include <Wy.stdio.h>
#include <Wy.unistd.h>
#include <Wy.signal.h>

using namespace Wy;

const char* LogFile="log.tmp";
RegFile logfile(LogFile,O_CREAT|O_TRUNC|O_WRONLY,S_IRUSR|S_IWUSR);

void sig_handler(int sig_num) {
SignaledContext sigcontext;
// File I/O (regular,character,socket,fifo,..) are async-signal safe.
try {
String str;

str << "sig_handler(" << sig_num << ") ";
logfile << str;
cout << str;
}
catch(const Errno& e) {
logfile << wrd(e) << ' ';
};
};

int main(int argc, const char* argv[]) try {
Errno r;
::signal(SIGINT,sig_handler);

logfile << '1';
if(argc) {
logfile << '2';
}
logfile << '3';

for(int i=0; i<3; ++i) {
::pause();
};
return 0;
}
catch(const Errno& e) {
cerr << wrd(e) << WY_ENDL;
return e.c_errno();
}
catch(...) {
cerr << "main() caught(...)" WY_ENDL;
throw;
};

Loading...