Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

sint.C

Go to the documentation of this file.
00001 
00002        //=======================================================//    _\|/_
00003       //  __  _____           ___                    ___       //      /|\
00004      //  /      |      ^     |   \  |         ^     |   \     //          _\|/_
00005     //   \__    |     / \    |___/  |        / \    |___/    //            /|\
00006    //       \   |    /___\   |  \   |       /___\   |   \   // _\|/_
00007   //     ___/   |   /     \  |   \  |____  /     \  |___/  //   /|\     
00008  //                                                       //            _\|/_
00009 //=======================================================//              /|\
00010 
00011 //
00012 // sint.C: Super Simon integer.
00013 //         Class definition for an integer long enough to count the
00014 //         number of orbits of a extremeley close binary (orbital period
00015 //         of hours to minutes [maybe even milliseconds]).
00016 //         Ment to be used in hdyn_unpert.C
00017 //
00018 //      S. Portegies Zwart et al.        12/1992
00019 //      S. Portegies Zeart                2/1997
00020 //
00021 //======================================================================
00022 
00023 #include "./sint.h"
00024 
00025 sint::sint(const char* digit_stream) {
00026   
00027   int n = strlen(digit_stream);
00028 
00029   sign = positive;
00030   
00031   if (n != 0) {
00032     
00033     digits = new char[ndigits=n];
00034     char* p = digits;
00035     const char* q = &digit_stream[n];
00036     while (n--) *p++ = *--q - '0';
00037     
00038   }
00039   else {
00040     
00041     digits = new char[ndigits=1];
00042     digits[0] = 0;
00043     
00044   }
00045 }
00046 
00047 sint::sint(int n) {
00048   
00049   char d[3*sizeof(int)+1];
00050   char* dp = d;
00051   ndigits = 0;
00052 
00053   sign = positive;
00054 
00055   do {
00056     
00057     *dp++ = n%10;
00058     n /= 10;
00059     ndigits++;
00060     
00061   } while (n > 0);
00062   
00063   digits = new char[ndigits];
00064   register int i;
00065   for (i=0; i<ndigits; i++) digits[i] = d[i];
00066   
00067 }
00068 
00069 sint::sint(real r) {
00070   
00071   ndigits = 0;
00072   sign = positive;
00073 
00074   real r_tmp = r;
00075 
00076   ndigits = int(floor(log10(r)+1));
00077   int precision = min(ndigits, NUMERICAL_PRECISION);
00078   
00079   digits = new char[ndigits];
00080 
00081   for(int i=0; i<ndigits; i++) {
00082     if(i<ndigits-precision) {
00083         digits[i] = 0;
00084     }
00085     else {
00086       r_tmp = r/pow(10., i+1);
00087       digits[i] = (int)floor(10*(r_tmp-floor(r_tmp)));
00088       r_tmp   -= floor(r_tmp);
00089     }
00090   }
00091 }
00092 
00093 sint::sint(const sint& n) {
00094   
00095   int i = n.ndigits;
00096   digits = new char[ndigits=i];
00097   char* p = digits;
00098   char* q = n.digits;
00099   while (i--) *p++ = *q++;
00100   
00101 }
00102 
00103 void sint::operator=(const sint& n) {
00104   
00105   if (this==&n) return;
00106   
00107   unsigned i = n.ndigits;
00108   digits = new char[ndigits=i];
00109   char* p = digits;
00110   char* q = n.digits;
00111   
00112   while (i--) *p++ = *q++;
00113 }
00114 
00115 sint sint::operator+(const sint& n) {
00116   
00117   int max_digits = (ndigits > n.ndigits ? ndigits : n.ndigits) + 1;
00118   
00119   char* psum = new char[max_digits];
00120   sint sum(psum, max_digits);
00121   digit_stream a(*this);
00122   digit_stream b(n);
00123   int i = max_digits;
00124   int carry = 0;
00125   
00126   while (i--) {
00127     
00128     *psum = (a++) + (b++) + carry;
00129     
00130     if (*psum>9) {
00131       
00132       carry = 1;
00133       *psum -= 10;
00134       
00135     }
00136     else carry = 0;
00137     
00138     psum++;
00139     
00140   }
00141 
00142   return sum;
00143 }
00144 
00145 void sint::print(ostream & s) const {
00146   
00147   int i;
00148 
00149   if (sign==negative)
00150      cout << "-";
00151   
00152   for (i=ndigits-1; i>=0; i--) printf("%d", digits[i]);
00153     //    s << digits[i];
00154   //  printf(s, "%d", digits[i]);
00155   //s << ToString();
00156 }
00157 
00158 void sint::chop()
00159 {
00160       int k;
00161       int len = NumDigits();
00162       for(k=len-1; k >= 0; k--)        // find a non-zero digit
00163         {
00164           cout << "digit "<<k<<" ="<< GetDigit(k)<<endl;
00165           if (GetDigit(k) != 0) break;
00166           ndigits--;               // "chop" off zeros
00167         }
00168       if (k < 0)    // all zeros
00169         {
00170           ndigits = 1;
00171           sign = positive;
00172         }
00173 }
00174 
00175 int sint::NumDigits() const
00176 // postcondition: returns # digits in sint
00177 {
00178     return ndigits;
00179 }
00180 
00181 int sint::GetDigit(int k) const
00182 // precondition: 0 <= k < NumDigits()
00183 // postcondition: returns k-th digit 
00184 //                (0 if precondition is false)
00185 //                Note: 0th digit is least significant digit
00186 {
00187     if (0 <= k && k < NumDigits())
00188     {
00189       cout << digits[k]-'0' << endl;
00190         return digits[k] - '0';
00191     }
00192     return 0;
00193 }
00194 
00195 // postcondition: returns true iff sint is negative
00196 bool sint::is_negative() const
00197 {
00198     return sign == negative;
00199 }
00200 
00201 bool sint::is_positive() const
00202 // postcondition: returns true iff sint is positive
00203 {
00204         return sign == positive;
00205 }
00206 bool sint::LessThan(const sint & rhs) const
00207 {
00208     // if signs aren't equal, self < rhs only if self is negative
00209     
00210     if (is_negative() != rhs.is_negative())
00211     {
00212         return is_negative();         
00213     }
00214 
00215     // if # digits aren't the same must check # digits and sign
00216     
00217     if (NumDigits() != rhs.NumDigits())
00218     {
00219         return (NumDigits() < rhs.NumDigits() && is_positive()) ||
00220                (NumDigits() > rhs.NumDigits() && is_negative());
00221     }
00222 
00223     // assert: # digits same, signs the same
00224 
00225     int k;
00226     int len = NumDigits();
00227 
00228     for(k=len-1; k >= 0; k--)
00229     {
00230         if (GetDigit(k) < rhs.GetDigit(k)) return is_positive();
00231         if (GetDigit(k) > rhs.GetDigit(k)) return is_negative();
00232     }
00233     cerr<<"Is it this false?"<<endl;
00234     return false;      // self == rhs
00235 }
00236 
00237 // Overloaded operators.
00238 
00239 // postcondition: return true if lhs < rhs, else returns false     
00240 bool operator < (const sint & lhs, const sint & rhs)
00241 {
00242     return lhs.LessThan(rhs);
00243 }
00244 
00245 bool operator > (const sint & lhs, const sint & rhs)
00246 // postcondition: return true if lhs > rhs, else returns false
00247 {
00248     return (rhs < lhs);    
00249 }
00250 
00251 ostream & operator <<(ostream & os, const sint & s)
00252 {
00253   for (int i=s.NumDigits()-1; i>=0; i--) {
00254     os << &s.digits[i]; 
00255   }
00256   
00257   return os;
00258 }
00259 
00260 

Generated at Sun Feb 24 09:57:16 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001