Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

xreal.h

Go to the documentation of this file.
00001 #ifndef  STARLAB_XREAL_H
00002 #  define  STARLAB_XREAL_H
00003 
00005 
00007 //                                              (SLWM, 5/00).
00008 
00018 //
00019 //         Permitted/defined operations:
00020 //
00021 //              initialized to 0 by default
00022 //              xreal = xreal
00023 //              xreal = int
00024 //              xreal = real
00025 //              int = xreal; Int(xreal), (int) xreal
00026 //              real = xreal; Real(xreal), (real) xreal
00027 //              -xreal
00028 //              xreal +/-/+=/-= xreal
00029 //              xreal +/-/+=/-= int
00030 //              xreal +/-/+=/-= real
00031 //              logical ==, !=, <, <=, >, >=
00032 //              <<, >>
00033 //
00034 
00035 //#define TWO64 (pow(2.0, 64))  // too inefficient!
00036 
00037 #define TWO64   (18446744073709551616.0)
00038 #define TWO63   ( 9223372036854775808.0)
00039 #define TWO64I  (5.42101086242752217e-20)
00040 #define TWO63M  ( 9223372036854775807)
00041 #define TWO63N  (-9223372036854775808)
00042 
00043 typedef long long               xint_t;
00044 typedef unsigned long long      xfrac_t;
00045 
00046 class xreal {
00047 
00048     private:
00049         xint_t i;                                       
00050         xfrac_t f;                                      
00051 
00052     public:
00053 
00054         // Constructors:
00055 
00056         xreal();                                        
00057 //      xreal(xreal y);                                 ///< x = y
00058         xreal(xint_t ii, xfrac_t ff);
00059         xreal(int ii);                                  
00060         xreal(real x);                                  
00061 
00062         // Accessors:
00063 
00064         xint_t get_i() {return i;}
00065         xfrac_t get_f() {return f;}
00066         real get_frac() {return TWO64I*f;}
00067 
00068         // Conversions:
00069 
00070         int to_int() {return i;}                        
00071         real to_real();                                 
00072 
00073         void print(ostream& s = cerr,
00074                    bool newline = false);               
00075 
00076         xreal::operator int() {return to_int();}
00077         xreal::operator real() {return to_real();}
00078 
00079         // Unary -, binary +, -, +=, -= (don't check for over/underflow):
00080 
00081         xreal  operator -  ();
00082         xreal  operator +  (const xreal y);
00083         xreal  operator -  (const xreal y);
00084         xreal& operator += (const xreal y);
00085         xreal& operator -= (const xreal y);
00086         xreal  operator +  (const real y);
00087         xreal  operator -  (const real y);
00088         xreal& operator += (const real y);
00089         xreal& operator -= (const real y);
00090 
00091         // Logical operators ==, !=, <, <=, >, >=:
00092         // (see also mixed operators below)
00093 
00094         bool operator == (const xreal y);
00095         bool operator != (const xreal y);
00096         bool operator <  (const xreal y);
00097         bool operator <= (const xreal y);
00098         bool operator >  (const xreal y);
00099         bool operator >= (const xreal y);
00100 
00101         friend istream & operator >> (ostream & , xreal &);
00102 };
00103 
00104 inline ostream & operator << (ostream & s, xreal x)
00105 {
00106     return s << (real)x;        // probably what we want most of the time
00107                                 // -- use x.xprint() for complete ouptut
00108 }
00109 
00110 istream & operator >> (istream & s, xreal & x);
00111 
00112 // Various real/xreal comparisons (rule: always promote to xreal).
00113 
00114 inline bool operator == (xreal x, real y) {return (x == (xreal)y);}
00115 inline bool operator == (real x, xreal y) {return ((xreal)x == y);}
00116 inline bool operator != (xreal x, real y) {return (x != (xreal)y);}
00117 inline bool operator != (real x, xreal y) {return ((xreal)x != y);}
00118 inline bool operator <  (xreal x, real y) {return (x < (xreal)y);}
00119 inline bool operator <  (real x, xreal y) {return ((xreal)x < y);}
00120 inline bool operator >  (xreal x, real y) {return (x > (xreal)y);}
00121 inline bool operator >  (real x, xreal y) {return ((xreal)x > y);}
00122 inline bool operator <= (xreal x, real y) {return (x <= (xreal)y);}
00123 inline bool operator <= (real x, xreal y) {return ((xreal)x <= y);}
00124 inline bool operator >= (xreal x, real y) {return (x >= (xreal)y);}
00125 inline bool operator >= (real x, xreal y) {return ((xreal)x >= y);}
00126 
00127 // Exclude the following definitions in favor of explicit casts if needed:
00128 //
00129 // inline real operator + (xreal x, real y) {return (real)(x + (xreal)y);}
00130 // inline real operator - (xreal x, real y) {return (real)(x - (xreal)y);}
00131 
00132 void put_real_number(ostream & s, char * label, xreal x);
00133 
00134 #endif

Generated on Wed Jul 20 12:43:37 2005 for Starlab by  doxygen 1.4.3