00001 00002 //=======================================================// _\|/_ 00003 // __ _____ ___ ___ // /|\ ~ 00004 // / | ^ | \ | ^ | \ // _\|/_ 00005 // \__ | / \ |___/ | / \ |___/ // /|\ ~ 00006 // \ | /___\ | \ | /___\ | \ // _\|/_ 00007 // ___/ | / \ | \ |____ / \ |___/ // /|\ ~ 00008 // // _\|/_ 00009 //=======================================================// /|\ ~ 00010 00012 00013 //---------------------------------------------------------------------- 00014 00015 // Include Doxygen introductory documentation here. 00016 00055 //---------------------------------------------------------------------- 00056 00057 // version 1: Dec 1992 Piet Hut, Steve McMillan, Jun Makino 00058 // 00059 // This file includes: 00060 // 1) new naming conventions to add to or replace existing names in C 00061 // 2) a string manipulation macro 00062 // 3) mathematical constants 00063 // 4) functions abs() min(,) max(,) 00064 // 5) macros to cast angular arguments in standard form 00065 00066 #ifndef STARLAB_STDINC_H 00067 # define STARLAB_STDINC_H 00068 00069 #include <cstdlib> // stdlib.h with namespace std 00070 #include <cmath> // math.h with namespace std 00071 #include <string> 00072 #include <fstream> 00073 00074 #ifdef HAVE_CONFIG_H 00075 # include "config.h" // config.h is created by "configure" (see autoconf) 00076 #endif 00077 00078 // New GNU, HP, and Sun all use stdiostream.h, which includes both 00079 // the stdio.h (C) and the iostream.h (C++) header files. SGI and 00080 // old GNU apparently want iostream.h and stdio.h to be included 00081 // separately... 00082 00083 #if defined(sgi) && !defined(__GNUC__) && !defined(SG_CPLUSPLUS) 00084 # define SG_CPLUSPLUS 1 00085 #endif 00086 00087 // Heck, for gcc3 this should do .... forget about the rest for now. 00088 #include <iostream> 00089 00090 // May not need include <cstdio> here?? 00091 #include <cstdio> 00092 00093 #if 0 00094 #if defined GNU_CPLUSPLUS 00095 # ifdef GNU_POST_29 00096 # include <iostream.h> 00097 # else 00098 # ifdef GNU_PRE_26 00099 # include <iostream.h> 00100 # include <stdio.h> 00101 # else 00102 # include <stdiostream.h> 00103 # endif 00104 # endif 00105 #elif defined SUN_CPLUSPLUS 00106 # include <stdiostream.h> 00107 #elif defined HP_CPLUSPLUS 00108 # include <stdiostream.h> 00109 #elif defined DEC_CPLUSPLUS 00110 # include <stdiostream.h> 00111 #elif defined SG_CPLUSPLUS 00112 # include <stdio.h> 00113 # include <iostream.h> 00114 #endif 00115 #endif 00116 00117 // So we get the good old standard cerr, cout, cin, .... 00118 00119 using namespace std; 00120 00121 //============================================================================= 00122 // New naming conventions to add to or replace existing names in C : 00123 //============================================================================= 00124 00125 //----------------------------------------------------------------------------- 00126 // real -- a more general name for the standard floating-point data type 00127 //----------------------------------------------------------------------------- 00128 00129 typedef double real; 00130 00131 //----------------------------------------------------------------------------- 00132 // bool -- another name for int, to indicate use in logical operations 00133 //----------------------------------------------------------------------------- 00134 00135 // g++ 2.6 (and later versions) already has a "bool" data type... 00136 00137 #if !defined(SG_CPLUSPLUS) && !defined(__GNUG__) 00138 typedef int bool; 00139 # define false 0 00140 # define true 1 00141 #endif 00142 00143 // Convenient definitions (the g++ >2.6 "bool" type has enumerated values 00144 // "false" and "true," which may be used interchangeably with these): 00145 00146 # define FALSE 0 00147 # define TRUE 1 00148 00149 //----------------------------------------------------------------------------- 00150 // local -- a more descriptive name for variables or functions which 00151 // are invisible outside the file in which they are defined. 00152 //----------------------------------------------------------------------------- 00153 00154 #define local static 00155 00156 //============================================================================= 00157 // A string manipulation macro : 00158 //============================================================================= 00159 00160 //----------------------------------------------------------------------------- 00161 // streq -- a macro which returns 1 if two strings are equal, 0 otherwise 00162 //----------------------------------------------------------------------------- 00163 00164 #define streq(x,y) (strcmp((x), (y)) == 0) 00165 00166 //============================================================================= 00167 // Simple output (#param is ANSI C, but presently only works with g"++ 00168 //============================================================================= 00169 00170 #define PRI(x) {for (int __pri__ = 0; __pri__ < x; __pri__++) cerr << " ";} 00171 00172 //#ifdef GNU_CPLUSPLUS 00173 00174 #define PR(x) cerr << #x << " = " << x << " " 00175 #define PRC(x) cerr << #x << " = " << x << ", " 00176 #define PRL(x) cerr << #x << " = " << x << endl 00177 00178 //#else 00179 00180 //#define PR(x) cerr << "x" << " = " << x << " " 00181 //#define PRC(x) cerr << "x" << " = " << x << ", " 00182 //#define PRL(x) cerr << "x" << " = " << x << endl 00183 00184 //#endif 00185 00186 //============================================================================= 00187 // Mathematical constants : 00188 //============================================================================= 00189 00190 //----------------------------------------------------------------------------- 00191 // pi, etc. -- mathematical constants, as well as `infinity' 00192 //----------------------------------------------------------------------------- 00193 00194 #ifndef PI 00195 # define PI 3.14159265358979323846 00196 #endif 00197 #ifndef M_PI 00198 # define M_PI PI 00199 #endif 00200 #define TWO_PI (2 * (PI)) 00201 #define HALF_PI (0.5 * (PI)) 00202 #define ONE_THIRD 0.33333333333333333333 00203 #define ONE_SIXTH 0.16666666666666666667 00204 00205 #define VERY_LARGE_NUMBER 1.0e300 00206 #define VERY_SMALL_NUMBER (pow(2.0, -52)) // dynamic limit on powers of 00207 // 2 for double precision 00208 00209 #define VERY_LARGE_INTEGER (1<<30) // assume we are working with 00210 // standard 4-byte integers, but 00211 // we also want the quantity 00212 // -VERY_LARGE_INTEGER to be legal 00213 00214 #define SETBIT(i, n) ((i) |= (1 << (n))) // set bit n of i 00215 #define GETBIT(i, n) ((i) & (1 << (n))) // get bit n of i 00216 #define CLRBIT(i, n) ((i) &= ~(1 << (n))) // clear bit n of i 00217 void printbits(unsigned int i); // print significant nonzero bits 00218 00219 //============================================================================= 00220 // Other constants : 00221 //============================================================================= 00222 00223 #define LOW_PRECISION 3 00224 #define STD_PRECISION 6 00225 #define INT_PRECISION 10 00226 #define HIGH_PRECISION 15 00227 00228 //============================================================================= 00229 // Functions abs() min(,) max(,) square() 00230 //============================================================================= 00231 // 00232 //----------------------------------------------------------------------------- 00233 // abs -- returns the absolute value of its argument 00234 // max -- returns the argument with the highest value 00235 // min -- returns the argument with the lowest value 00236 // square - returns the square of its argument (cf. vec version) 00237 //----------------------------------------------------------------------------- 00238 00239 namespace Starlab { 00240 #ifndef HP_CPLUSPLUS 00241 inline real abs(real x) {return (x < 0) ? -x : x;} 00242 inline long int abs(long int x) {return (x < 0) ? -x : x;} 00243 # ifndef GNU_CPLUSPLUS 00244 # ifndef SG_CPLUSPLUS 00245 inline int abs(int x) {return (x < 0) ? -x : x;} 00246 # endif 00247 # endif 00248 #endif 00249 } 00250 00251 #ifdef _WIN32 00252 extern double erf(double); 00253 extern double erfc(double); 00254 00255 inline real rint(real x) {return floor((x) + 0.5);} 00256 #endif 00257 00258 #undef max 00259 #undef min 00260 00261 namespace Starlab { 00262 00263 inline real max(real x, real y) {return (x > y) ? x : y;} 00264 inline int max(int x, int y) {return (x > y) ? x : y;} 00265 inline long int max(long int x, long int y) {return (x > y) ? x : y;} 00266 00267 inline real min(real x, real y) {return (x < y) ? x : y;} 00268 inline int min(int x, int y) {return (x < y) ? x : y;} 00269 inline long int min(long int x, long int y) {return (x < y) ? x : y;} 00270 00271 // Force mixed "max" and "min" calculations to be done as real, to 00272 // avoid "ambiguous" errors from g++ 00273 00274 #ifdef GNU_PRE_26 00275 inline real max(int x, real y) {return (x > y) ? x : y;} 00276 inline real max(real x, int y) {return (x > y) ? x : y;} 00277 00278 inline real min(real x, int y) {return (x < y) ? x : y;} 00279 inline real min(int x, real y) {return (x < y) ? x : y;} 00280 #endif 00281 } 00282 00283 inline real square(real x) {return x*x;} 00284 00285 //============================================================================= 00286 // Macros to cast angular arguments in standard form : 00287 //============================================================================= 00288 00289 //----------------------------------------------------------------------------- 00290 // pos_angle -- recasts an angular variable into the range [0, TWO_PI) 00291 // sym_angle -- recasts an angular variable into the range [-PI, PI) 00292 // (recasting: transforming modulo 2 pi) 00293 // example: 00294 // to map an angular variable 'phi' into the smallest positive 00295 // value, use 00296 // 00297 // phi = pos_angle(phi); 00298 // 00299 // to map an angular variable 'phi' into the smallest value, 00300 // positive or negative, use 00301 // 00302 // phi = sym_angle(phi); 00303 // 00304 //----------------------------------------------------------------------------- 00305 00306 #define pos_angle(phi) ((phi) - TWO_PI * floor((phi)/TWO_PI )) 00307 #define sym_angle(phi) ((phi) - TWO_PI * floor(((phi)+PI)/TWO_PI )) 00308 00309 //============================================================================= 00310 // xreal -- extended precision real, for use with time and system_time 00311 //============================================================================= 00312 00313 // Long long is an ongoing irritant... See std/xreal.C 00314 // *** Options should now be properly set up by configure. (Steve, 9/04) *** 00315 00316 #if 0 // old (pre-/04) 00317 00318 # ifdef HAVE_STRTOLL 00319 00320 // e.g. Linux. 00321 00322 # define STRTOL strtoll 00323 # define STRTOUL strtoull 00324 00325 # else 00326 00327 // e.g. Dec UNIX 00328 00329 # define STRTOL strtol 00330 # define STRTOUL strtoul 00331 00332 # endif 00333 00334 #else // new 00335 00336 // Configure should have done all the work, but just in case the size of 00337 // a long long isn't 8 bytes, silently disable USE_XREAL. (If this has 00338 // been handled properly, configure should already have warned the user.) 00339 00340 # ifdef USE_XREAL 00341 00342 // Make sure... 00343 00344 # ifdef HAVE_LONG_LONG 00345 # ifdef SIZEOF_LONG_LONG 00346 # if (SIZEOF_LONG_LONG != 8) 00347 //# warning "Undefining USE_XREAL because long long size is not 8" 00348 # undef USE_XREAL 00349 # endif 00350 # endif 00351 # else 00352 //# warning "Undefining USE_XREAL because long long is not defined" 00353 # undef USE_XREAL 00354 # endif 00355 # 00356 # endif 00357 00358 #endif 00359 00360 #ifdef USE_XREAL 00361 # include "xreal.h" 00362 #else 00363 typedef real xreal; 00364 #endif 00365 00366 #ifndef HAVE_STPCPY 00367 char *stpcpy(char *restrict, const char *restrict); 00368 #endif 00369 00370 // Intended for xreal, but referenced in the real version too. 00371 00372 void xprint(xreal x, 00373 ostream & s = cerr, 00374 bool newline = true); 00375 real fmod2(xreal x, real y); 00376 xreal get_xreal(char *str); 00377 void identify_xreal(ostream& s = cerr); 00378 00379 //============================================================================= 00380 // Various declarations 00381 //============================================================================= 00382 00383 void print_message(char*); 00384 void warning(char*); 00385 void err_exit(char*); 00386 00387 int pgetopt(int argc, char** argv, char *optstr, 00388 char *cvs_id = NULL, char *source = NULL); 00389 00390 void pskipopt(); 00391 void params_to_usage(ostream&, char*, char*); 00392 00393 int srandinter(int, int n = 0); 00394 int get_initial_seed(); 00395 int get_rand_seed(); 00396 int get_current_seed(); 00397 int get_n_rand(); 00398 real randinter(real, real); 00399 real gausrand(real, real); 00400 00401 void cpu_init(); 00402 real cpu_time(); 00403 00404 void starlab_wait(int iwait); 00405 00406 int set_starlab_precision(ostream&); 00407 int adjust_starlab_precision(int p); 00408 int get_starlab_precision(); 00409 00410 char * gethist(int, char **); 00411 00412 // Convenient invocation of run-time help function. 00413 00414 void check_runtime_help(int argc, char** argv, 00415 char* source_file, char* date, char *time); 00416 void get_runtime_help(char* source_file, char* date, char *time, int level = 1); 00417 00418 // Note: assuming here that the macros __DATE__ and __TIME__ are standard... 00419 // Macro _SRC_ will be provided by configure/make. 00420 00421 #define check_help() check_runtime_help(argc, argv, _SRC_, __DATE__, __TIME__); 00422 #define get_help() get_runtime_help(_SRC_, __DATE__, __TIME__); 00423 00424 #endif 00425 00426 //=======================================================================// 00427 // +---------------+ _\|/_ +------------------------------\\ ~ 00428 // | the end of: | /|\ | inc/stdinc.h 00429 // +---------------+ +------------------------------// 00430 //========================= STARLAB =====================================\\ ~