Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #121001 > unrolled thread

Interesting results

Started byBonita Montero <Bonita.Montero@gmail.com>
First post2024-11-24 17:29 +0100
Last post2024-11-26 03:41 +0800
Articles 7 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Interesting results Bonita Montero <Bonita.Montero@gmail.com> - 2024-11-24 17:29 +0100
    Re: Interesting results Paavo Helde <eesnimi@osa.pri.ee> - 2024-11-25 00:25 +0200
      Re: Interesting results Bonita Montero <Bonita.Montero@gmail.com> - 2024-11-25 06:32 +0100
    Re: Interesting results Michael S <already5chosen@yahoo.com> - 2024-11-25 01:04 +0200
      Re: Interesting results Bonita Montero <Bonita.Montero@gmail.com> - 2024-11-25 06:32 +0100
    Re: Interesting results wij <wyniijj5@gmail.com> - 2024-11-26 03:32 +0800
      Re: Interesting results wij <wyniijj5@gmail.com> - 2024-11-26 03:41 +0800

#121001 — Interesting results

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-11-24 17:29 +0100
SubjectInteresting results
Message-ID<vhvk6i$2a5nc$1@raubtier-asyl.eternal-september.org>
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 !

[toc] | [next] | [standalone]


#121003

FromPaavo Helde <eesnimi@osa.pri.ee>
Date2024-11-25 00:25 +0200
Message-ID<vi093s$2dle7$1@dont-email.me>
In reply to#121001
On 24.11.2024 18:29, Bonita Montero wrote:
> #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;

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.



>              }
>          }
>      }
> }

[toc] | [prev] | [next] | [standalone]


#121011

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-11-25 06:32 +0100
Message-ID<vi1242$2l3v8$2@raubtier-asyl.eternal-september.org>
In reply to#121003
Am 24.11.2024 um 23:25 schrieb Paavo Helde:
 > 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.

Yes, I accidentally used the <-operator only.

[toc] | [prev] | [next] | [standalone]


#121004

FromMichael S <already5chosen@yahoo.com>
Date2024-11-25 01:04 +0200
Message-ID<20241125010452.00006c08@yahoo.com>
In reply to#121001
On Sun, 24 Nov 2024 17:29:00 +0100
Bonita Montero <Bonita.Montero@gmail.com> wrote:

> 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 !
> 

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 ;





[toc] | [prev] | [next] | [standalone]


#121010

FromBonita Montero <Bonita.Montero@gmail.com>
Date2024-11-25 06:32 +0100
Message-ID<vi122n$2l3v8$1@raubtier-asyl.eternal-september.org>
In reply to#121004
Am 25.11.2024 um 00:04 schrieb 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.

> Here is the same test written in more sane language and style. Results
> are 100% as expected:

Your code is less readable for me.

[toc] | [prev] | [next] | [standalone]


#121013

Fromwij <wyniijj5@gmail.com>
Date2024-11-26 03:32 +0800
Message-ID<ad2ab6d192a7614cd923b30914a3a176de5018d1.camel@gmail.com>
In reply to#121001
On Sun, 2024-11-24 at 17:29 +0100, Bonita Montero wrote:
> 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 !
> 

#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
		>=: 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
OK

------------
Not interesting !

[toc] | [prev] | [next] | [standalone]


#121014

Fromwij <wyniijj5@gmail.com>
Date2024-11-26 03:41 +0800
Message-ID<e201a745af9f268306bdedfd9d765ed3ca24094d.camel@gmail.com>
In reply to#121013
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;
};

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web