home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / Rational.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  8KB  |  291 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /* 
  4. Copyright (C) 1988 Free Software Foundation
  5.     written by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of the GNU C++ Library.  This library is free
  8. software; you can redistribute it and/or modify it under the terms of
  9. the GNU Library General Public License as published by the Free
  10. Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  This library is distributed in the hope
  12. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. PURPOSE.  See the GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #ifndef _Rational_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _Rational_h 1
  25.  
  26. #include <Integer.h>
  27. #include <math.h>
  28.  
  29. #undef OK
  30.  
  31. class Rational
  32. {
  33. protected:
  34.   Integer          num;
  35.   Integer          den;
  36.  
  37.   void             normalize();
  38.  
  39. public:
  40.                    Rational();
  41.                    Rational(double);
  42.                    Rational(int n);
  43.                    Rational(long n);
  44.                    Rational(int n, int d);
  45.                    Rational(long n, long d);
  46.                    Rational(long n, unsigned long d);
  47.                    Rational(unsigned long n, long d);
  48.                    Rational(unsigned long n, unsigned long d);
  49.                    Rational(const Integer& n);
  50.                    Rational(const Integer& n, const Integer& d);
  51.                    Rational(const Rational&);
  52.  
  53.                   ~Rational();
  54.  
  55.   Rational&        operator =  (const Rational& y);
  56.  
  57.   friend int       operator == (const Rational& x, const Rational& y);
  58.   friend int       operator != (const Rational& x, const Rational& y);
  59.   friend int       operator <  (const Rational& x, const Rational& y);
  60.   friend int       operator <= (const Rational& x, const Rational& y);
  61.   friend int       operator >  (const Rational& x, const Rational& y);
  62.   friend int       operator >= (const Rational& x, const Rational& y);
  63.  
  64.   friend Rational  operator +  (const Rational& x, const Rational& y);
  65.   friend Rational  operator -  (const Rational& x, const Rational& y);
  66.   friend Rational  operator *  (const Rational& x, const Rational& y);
  67.   friend Rational  operator /  (const Rational& x, const Rational& y);
  68.  
  69.   Rational&        operator += (const Rational& y);
  70.   Rational&        operator -= (const Rational& y);
  71.   Rational&        operator *= (const Rational& y);
  72.   Rational&        operator /= (const Rational& y);
  73.  
  74. #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
  75.   friend Rational  operator <? (const Rational& x, const Rational& y); // min
  76.   friend Rational  operator >? (const Rational& x, const Rational& y); // max
  77. #endif
  78.  
  79.   friend Rational  operator - (const Rational& x);
  80.  
  81.  
  82. // builtin Rational functions
  83.  
  84.  
  85.   void             negate();                      // x = -x
  86.   void             invert();                      // x = 1/x
  87.  
  88.   friend int       sign(const Rational& x);             // -1, 0, or +1
  89.   friend Rational  abs(const Rational& x);              // absolute value
  90.   friend Rational  sqr(const Rational& x);              // square
  91.   friend Rational  pow(const Rational& x, long y);
  92.   friend Rational  pow(const Rational& x, const Integer& y);
  93.   const Integer&   numerator() const;
  94.   const Integer&   denominator() const;
  95.  
  96. // coercion & conversion
  97.  
  98.                    operator double() const;
  99.   friend Integer   floor(const Rational& x);
  100.   friend Integer   ceil(const Rational& x);
  101.   friend Integer   trunc(const Rational& x);
  102.   friend Integer   round(const Rational& x);
  103.  
  104.   friend istream&  operator >> (istream& s, Rational& y);
  105.   friend ostream&  operator << (ostream& s, const Rational& y);
  106.  
  107.   int           fits_in_float() const;
  108.   int           fits_in_double() const;
  109.  
  110. // procedural versions of operators
  111.  
  112.   friend int       compare(const Rational& x, const Rational& y);
  113.   friend void      add(const Rational& x, const Rational& y, Rational& dest);
  114.   friend void      sub(const Rational& x, const Rational& y, Rational& dest);
  115.   friend void      mul(const Rational& x, const Rational& y, Rational& dest);
  116.   friend void      div(const Rational& x, const Rational& y, Rational& dest);
  117.  
  118. // error detection
  119.  
  120.   void    error(const char* msg) const;
  121.   int              OK() const;
  122.  
  123. };
  124.  
  125. typedef Rational RatTmp; // backwards compatibility
  126.  
  127. inline Rational::Rational() : num(&_ZeroRep), den(&_OneRep) {}
  128. inline Rational::~Rational() {}
  129.  
  130. inline Rational::Rational(const Rational& y) :num(y.num), den(y.den) {}
  131.  
  132. inline Rational::Rational(const Integer& n) :num(n), den(&_OneRep) {}
  133.  
  134. inline Rational::Rational(const Integer& n, const Integer& d) :num(n),den(d)
  135. {
  136.   normalize();
  137. }
  138.  
  139. inline Rational::Rational(long n) :num(n), den(&_OneRep) { }
  140.  
  141. inline Rational::Rational(int n) :num(n), den(&_OneRep) { }
  142.  
  143. inline Rational::Rational(long n, long d) :num(n), den(d) { normalize(); }
  144. inline Rational::Rational(int n, int d) :num(n), den(d) { normalize(); }
  145. inline Rational::Rational(long n, unsigned long d) :num(n), den(d)
  146. {
  147.   normalize();
  148. }
  149. inline Rational::Rational(unsigned long n, long d) :num(n), den(d)
  150. {
  151.   normalize();
  152. }
  153. inline Rational::Rational(unsigned long n, unsigned long d) :num(n), den(d)
  154. {
  155.   normalize();
  156. }
  157.  
  158. inline Rational& Rational::operator =  (const Rational& y)
  159. {
  160.   num = y.num;  den = y.den;
  161.   return *this;
  162. }
  163.  
  164. inline int operator == (const Rational& x, const Rational& y)
  165. {
  166.   return compare(x.num, y.num) == 0 && compare(x.den, y.den) == 0;
  167. }
  168.  
  169. inline int operator != (const Rational& x, const Rational& y)
  170. {
  171.   return compare(x.num, y.num) != 0 || compare(x.den, y.den) != 0;
  172. }
  173.  
  174. inline int operator <  (const Rational& x, const Rational& y)
  175. {
  176.   return compare(x, y) <  0; 
  177. }
  178.  
  179. inline int operator <= (const Rational& x, const Rational& y)
  180. {
  181.   return compare(x, y) <= 0; 
  182. }
  183.  
  184. inline int operator >  (const Rational& x, const Rational& y)
  185. {
  186.   return compare(x, y) >  0; 
  187. }
  188.  
  189. inline int operator >= (const Rational& x, const Rational& y)
  190. {
  191.   return compare(x, y) >= 0; 
  192. }
  193.  
  194. inline int sign(const Rational& x)
  195. {
  196.   return sign(x.num);
  197. }
  198.  
  199. inline void Rational::negate()
  200. {
  201.   num.negate();
  202. }
  203.  
  204.  
  205. inline Rational& Rational::operator += (const Rational& y) 
  206. {
  207.   add(*this, y, *this);
  208.   return *this;
  209. }
  210.  
  211. inline Rational& Rational::operator -= (const Rational& y) 
  212. {
  213.   sub(*this, y, *this);
  214.   return *this;
  215. }
  216.  
  217. inline Rational& Rational::operator *= (const Rational& y) 
  218. {
  219.   mul(*this, y, *this);
  220.   return *this;
  221. }
  222.  
  223. inline Rational& Rational::operator /= (const Rational& y) 
  224. {
  225.   div(*this, y, *this);
  226.   return *this;
  227. }
  228.  
  229. inline const Integer& Rational::numerator() const { return num; }
  230. inline const Integer& Rational::denominator() const { return den; }
  231. inline Rational::operator double() const { return ratio(num, den); }
  232.  
  233. #if defined (__GNUG__) && ! defined (__STRICT_ANSI__)
  234. inline Rational operator <? (const Rational& x, const Rational& y)
  235. {
  236.   if (compare(x, y) <= 0) return x; else return y;
  237. }
  238.  
  239. inline Rational operator >? (const Rational& x, const Rational& y)
  240. {
  241.   if (compare(x, y) >= 0) return x; else return y;
  242. }
  243. #endif
  244.  
  245. #if defined(__GNUG__) && !defined(_G_NO_NRV)
  246.  
  247. inline Rational operator + (const Rational& x, const Rational& y) return r
  248. {
  249.   add(x, y, r);
  250. }
  251.  
  252. inline Rational operator - (const Rational& x, const Rational& y) return r
  253. {
  254.   sub(x, y, r);
  255. }
  256.  
  257. inline Rational operator * (const Rational& x, const Rational& y) return r
  258. {
  259.   mul(x, y, r);
  260. }
  261.  
  262. inline Rational operator / (const Rational& x, const Rational& y) return r
  263. {
  264.   div(x, y, r);
  265. }
  266.  
  267. #else /* NO_NRV */
  268.  
  269. inline Rational operator + (const Rational& x, const Rational& y) 
  270. {
  271.   Rational r; add(x, y, r); return r;
  272. }
  273.  
  274. inline Rational operator - (const Rational& x, const Rational& y)
  275. {
  276.   Rational r; sub(x, y, r); return r;
  277. }
  278.  
  279. inline Rational operator * (const Rational& x, const Rational& y)
  280. {
  281.   Rational r; mul(x, y, r); return r;
  282. }
  283.  
  284. inline Rational operator / (const Rational& x, const Rational& y)
  285. {
  286.   Rational r; div(x, y, r); return r;
  287. }
  288. #endif
  289.  
  290. #endif
  291.