From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,94f5b26bc297a928 X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,7a6a623afb38d7f7 X-Google-Attributes: gid103376,public X-Google-Thread: 1094ba,7a6a623afb38d7f7 X-Google-Attributes: gid1094ba,public From: Constantine Antonovich: Subject: Re: Fortran vs C++ vs. etc (has little to do with realtime anymore) Date: 1997/09/25 Message-ID: <342A1486.617C@orbotech.co.il> X-Deja-AN: 275381126 Cache-Post-Path: dns!unknown@frank References: <5ve7c6$f4m$1@info.uah.edu> <5vmbdl$v8f$1@news.iastate.edu> <5vu47d$ea8$1@news.iastate.edu> X-Complaints-To: usenet@news.NetVision.net.il X-Trace: news.NetVision.net.il 875173007 1201 (None) 199.203.54.100 Organization: Orbotech ltd. Newsgroups: comp.lang.c++,comp.lang.fortran,comp.lang.ada Date: 1997-09-25T00:00:00+00:00 List-Id: Oleg Krivosheev wrote: > > [skipped] > > sorry, i have only min ;) > > well, try example below for min with > arbitrary larger number of arguments. Hope it can help > > #include > #include > #include > > double > dmin( int nofargs, ... ) { > > double res = 1.0e+38, arg; > int j; > va_list ap; > > assert( nofargs > 1 ); > > va_start(ap, nofargs ); > > for( j = 0; j < nofargs; ++j ) { > arg=va_arg( ap, double ); > if ( arg < res ) { > res = arg; > } > } > va_end(ap); > return res; > } > > int > main( void ) { > > printf( "%e\n", dmin( 2, 1.0, 2.0 ) ); > > printf( "%e\n", dmin( 3, 0.0, 1.0, 2.0 ) ); > > printf( "%e\n", dmin( 5, 0.0, 1.0, 2.0, 7.0, -0.2 ) ); > > return 0; > } > > [skipped] > OK, just to be pedantic, the previous example shows very fine the difference between fortran and C in computations. Since min/max are standard fortran functions, it can be supposed they are maximally optimized with no regards to number of their arguments (just because the compiler takes care of them). In the contrary, in the example, we have overhead of the arguments number evaluation and overhead of implementation (not necessarily this one, generally almost any non-trivial non-built-in operation has some overhead - there has been some reason to add into C libraries memcpy, memcmp and so on functions). Moreover the idea of such an implementation contains two great places to make bugs and definitely any user of such an implementation will be doing the bugs endlessly. Firstly, it is the programmer who should worry about conformity of the declared number of arguments to the actual one. In such cases, generally, adding or removing actual arguments, people tend to forget to correct the declared number. Secondly, there is no way for type checking and for type conversions, usage of 1 instead of 1.0 in the arguments list will cause very nasty problems. Using C++ and not C, most probably, I would try to implement the min function in the following way: inline double min(double v1, double v2) { return v1 #include #include extern double dmin(int, double, ...); //------------------------------------------------------ inline double min(double v1, double v2) { return v1 double dmin(int nofargs, double d, ...) { va_list ap; va_start(ap,d); while (--nofargs) { double v=va_arg(ap,double); if (v