home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / Rational.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  8KB  |  282 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, 675 Mass Ave, Cambridge, MA 02139, 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. class Rational
  30. {
  31. protected:
  32.   Integer          num;
  33.   Integer          den;
  34.  
  35.   void             normalize();
  36.  
  37. public:
  38.                    Rational();
  39.                    Rational(double);
  40.                    Rational(long n);
  41.                    Rational(long n, long d);
  42.                    Rational(long n, unsigned long d);
  43.                    Rational(unsigned long n, long d);
  44.                    Rational(unsigned long n, unsigned long d);
  45.                    Rational(const Integer& n);
  46.                    Rational(const Integer& n, const Integer& d);
  47.                    Rational(const Rational&);
  48.  
  49.                   ~Rational();
  50.  
  51.   void             operator =  (const Rational& y);
  52.  
  53.   friend int       operator == (const Rational& x, const Rational& y);
  54.   friend int       operator != (const Rational& x, const Rational& y);
  55.   friend int       operator <  (const Rational& x, const Rational& y);
  56.   friend int       operator <= (const Rational& x, const Rational& y);
  57.   friend int       operator >  (const Rational& x, const Rational& y);
  58.   friend int       operator >= (const Rational& x, const Rational& y);
  59.  
  60.   friend Rational  operator +  (const Rational& x, const Rational& y);
  61.   friend Rational  operator -  (const Rational& x, const Rational& y);
  62.   friend Rational  operator *  (const Rational& x, const Rational& y);
  63.   friend Rational  operator /  (const Rational& x, const Rational& y);
  64.  
  65.   void             operator += (const Rational& y);
  66.   void             operator -= (const Rational& y);
  67.   void             operator *= (const Rational& y);
  68.   void             operator /= (const Rational& y);
  69.  
  70. #ifdef __GNUG__
  71.   friend Rational  operator <? (const Rational& x, const Rational& y); // min
  72.   friend Rational  operator >? (const Rational& x, const Rational& y); // max
  73. #endif
  74.  
  75.   friend Rational  operator - (const Rational& x);
  76.  
  77.  
  78. // builtin Rational functions
  79.  
  80.  
  81.   void             negate();                      // x = -x
  82.   void             invert();                      // x = 1/x
  83.  
  84.   friend int       sign(const Rational& x);             // -1, 0, or +1
  85.   friend Rational  abs(const Rational& x);              // absolute value
  86.   friend Rational  sqr(const Rational& x);              // square
  87.   friend Rational  pow(const Rational& x, long y);
  88.   friend Rational  pow(const Rational& x, const Integer& y);
  89.   const Integer&   numerator() const;
  90.   const Integer&   denominator() const;
  91.  
  92. // coercion & conversion
  93.  
  94.                    operator double() const;
  95.   friend Integer   floor(const Rational& x);
  96.   friend Integer   ceil(const Rational& x);
  97.   friend Integer   trunc(const Rational& x);
  98.   friend Integer   round(const Rational& x);
  99.  
  100.   friend istream&  operator >> (istream& s, Rational& y);
  101.   friend ostream&  operator << (ostream& s, const Rational& y);
  102.  
  103.   int           fits_in_float() const;
  104.   int           fits_in_double() const;
  105.  
  106. // procedural versions of operators
  107.  
  108.   friend int       compare(const Rational& x, const Rational& y);
  109.   friend void      add(const Rational& x, const Rational& y, Rational& dest);
  110.   friend void      sub(const Rational& x, const Rational& y, Rational& dest);
  111.   friend void      mul(const Rational& x, const Rational& y, Rational& dest);
  112.   friend void      div(const Rational& x, const Rational& y, Rational& dest);
  113.  
  114. // error detection
  115.  
  116.   void    error(const char* msg) const;
  117.   int              OK() const;
  118.  
  119. };
  120.  
  121. typedef Rational RatTmp; // backwards compatibility
  122.  
  123. inline Rational::Rational() : num(&_ZeroRep), den(&_OneRep) {}
  124. inline Rational::~Rational() {}
  125.  
  126. inline Rational::Rational(const Rational& y) :num(y.num), den(y.den) {}
  127.  
  128. inline Rational::Rational(const Integer& n) :num(n), den(&_OneRep) {}
  129.  
  130. inline Rational::Rational(const Integer& n, const Integer& d) :num(n),den(d)
  131. {
  132.   normalize();
  133. }
  134.  
  135. inline Rational::Rational(long n) :num(n), den(&_OneRep) { }
  136.  
  137. inline Rational::Rational(long n, long d) :num(n), den(d)
  138. {
  139.   normalize();
  140. }
  141. inline Rational::Rational(long n, unsigned long d) :num(n), den(d)
  142. {
  143.   normalize();
  144. }
  145. inline Rational::Rational(unsigned long n, long d) :num(n), den(d)
  146. {
  147.   normalize();
  148. }
  149. inline Rational::Rational(unsigned long n, unsigned long d) :num(n), den(d)
  150. {
  151.   normalize();
  152. }
  153.  
  154. inline  void Rational::operator =  (const Rational& y)
  155. {
  156.   num = y.num;  den = y.den;
  157. }
  158.  
  159. inline int operator == (const Rational& x, const Rational& y)
  160. {
  161.   return compare(x.num, y.num) == 0 && compare(x.den, y.den) == 0;
  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, y) <  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 sign(const Rational& x)
  190. {
  191.   return sign(x.num);
  192. }
  193.  
  194. inline void Rational::negate()
  195. {
  196.   num.negate();
  197. }
  198.  
  199.  
  200. inline void Rational::operator += (const Rational& y) 
  201. {
  202.   add(*this, y, *this);
  203. }
  204.  
  205. inline void Rational::operator -= (const Rational& y) 
  206. {
  207.   sub(*this, y, *this);
  208. }
  209.  
  210. inline void Rational::operator *= (const Rational& y) 
  211. {
  212.   mul(*this, y, *this);
  213. }
  214.  
  215. inline void Rational::operator /= (const Rational& y) 
  216. {
  217.   div(*this, y, *this);
  218. }
  219.  
  220. inline const Integer& Rational::numerator() const { return num; }
  221. inline const Integer& Rational::denominator() const { return den; }
  222. inline Rational::operator double() const { return ratio(num, den); }
  223.  
  224. #ifdef __GNUG__
  225. inline Rational operator <? (const Rational& x, const Rational& y)
  226. {
  227.   if (compare(x, y) <= 0) return x; else return y;
  228. }
  229.  
  230. inline Rational operator >? (const Rational& x, const Rational& y)
  231. {
  232.   if (compare(x, y) >= 0) return x; else return y;
  233. }
  234. #endif
  235.  
  236. #if defined(__GNUG__) && !defined(NO_NRV)
  237.  
  238. inline Rational operator + (const Rational& x, const Rational& y) return r
  239. {
  240.   add(x, y, r);
  241. }
  242.  
  243. inline Rational operator - (const Rational& x, const Rational& y) return r
  244. {
  245.   sub(x, y, r);
  246. }
  247.  
  248. inline Rational operator * (const Rational& x, const Rational& y) return r
  249. {
  250.   mul(x, y, r);
  251. }
  252.  
  253. inline Rational operator / (const Rational& x, const Rational& y) return r
  254. {
  255.   div(x, y, r);
  256. }
  257.  
  258. #else /* NO_NRV */
  259.  
  260. inline Rational operator + (const Rational& x, const Rational& y) 
  261. {
  262.   Rational r; add(x, y, r); return r;
  263. }
  264.  
  265. inline Rational operator - (const Rational& x, const Rational& y)
  266. {
  267.   Rational r; sub(x, y, r); return r;
  268. }
  269.  
  270. inline Rational operator * (const Rational& x, const Rational& y)
  271. {
  272.   Rational r; mul(x, y, r); return r;
  273. }
  274.  
  275. inline Rational operator / (const Rational& x, const Rational& y)
  276. {
  277.   Rational r; div(x, y, r); return r;
  278. }
  279. #endif
  280.  
  281. #endif
  282.