text: Simulations work better with real numbers, and there is a Real type and some macros we use in our programs, so we're going to have a file called RealNumber.h which we include in our files using real numbers. (If we didn't want to do all those includes, we could have put the #include "realnumber.h" somewhere inside of the StdAfx.h file, for instance right before the line that says //{{AFX_INSERT_LOCATION}}.)
  In our code we use Real as another word for double or for float. To give ourselves the freedom to switch between double and float, depending on whether we have a greater need for accuracy or for speed, we have a line saying typedef double Real or typedef float Real inside RealNumber.h.
  #ifndef REALNUMBER_H #define REALNUMBER_H /* Note that to use the Real type throughout my program, I have to always remember to include realnumber.h. */ #define USEFLOAT // float is slightly faster, slightly less accurate. #ifdef USEFLOAT typedef float Real; #else //not USEFLOAT typedef double Real; #endif //USEFLOAT /* When I use a float Real, I'll get warning messages generated by the fact that constants like 2.0 are viewed as doubles, so that if x is a float, the line x = x/2.0 would be viewed as converting a double to a float. These messages take this form: warning C4244: '=' : conversion from 'double' to 'float', possible loss of data warning C4305: '=' : truncation from 'const double' to 'float' I can disable these warning messages with a pragma stating the two warning numbers. */ #pragma warning(disable: 4305 4244) #define PI 3.14159265358979 #define CLAMP(x,lo,hi) (x)=(((x)<(lo))?(lo):(((x)>(hi))?(hi):(x))) #define SMALL_REAL 0.00001 #define BIG_REAL 1000000000.0 #endif //REALNUMBER_H
  The point of the typedef double Real line is that if at some point you find that you want to squeeze a little more speed out of our program, you can come back here and replace double by float. We need PI because, although the ANSI C library is supposed to define an M_PI constant of this value in math.h, the Microsoft version of the math.h file doesn't seem to have any pi in it. (For sanity's sake, a software engineer learns not to obsess over things like this; simply fix them and move on.) The CLAMP macro is useful for forcing a variable x to lie between two values lo and hi. SMALL_REAL is convenient in code where you want to avoid dividing by numbers that are too close to 0. And BIG_REAL is useful to put in as a starting value in a search loop when we keep looking for a smaller number, as in the code for cCritter* cBiota::pickClosest(const cVector &vclick) in biota.cpp for instance.