home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__inc / xrationa.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  7.0 KB  |  266 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 once
  23. #pragma interface
  24. #endif
  25. #define _Rational_h 1
  26.  
  27. #include <xinteger.h>
  28. #include <math.h>
  29.  
  30. class Rational
  31. {
  32. protected:
  33.   Integer          num;
  34.   Integer          den;
  35.  
  36.   void             normalize();
  37.  
  38. public:
  39.                    Rational();
  40.                    Rational(double);
  41.                    Rational(long n, long d = 1);
  42.                    Rational(const Integer& n);
  43.                    Rational(const Integer& n, const Integer& d);
  44.                    Rational(const Rational&);
  45.  
  46.                   ~Rational();
  47.  
  48.   void             operator =  (const Rational& y);
  49.  
  50.   friend int       operator == (const Rational& x, const Rational& y);
  51.   friend int       operator != (const Rational& x, const Rational& y);
  52.   friend int       operator <  (const Rational& x, const Rational& y);
  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.  
  57.   friend Rational  operator +  (const Rational& x, const Rational& y);
  58.   friend Rational  operator -  (const Rational& x, const Rational& y);
  59.   friend Rational  operator *  (const Rational& x, const Rational& y);
  60.   friend Rational  operator /  (const Rational& x, const Rational& y);
  61.  
  62.   void             operator += (const Rational& y);
  63.   void             operator -= (const Rational& y);
  64.   void             operator *= (const Rational& y);
  65.   void             operator /= (const Rational& y);
  66.  
  67. #ifdef __GNUG__
  68.   friend Rational  operator <? (const Rational& x, const Rational& y); // min
  69.   friend Rational  operator >? (const Rational& x, const Rational& y); // max
  70. #endif
  71.  
  72.   friend Rational  operator - (const Rational& x);
  73.  
  74.  
  75. // builtin Rational functions
  76.  
  77.  
  78.   void             negate();                      // x = -x
  79.   void             invert();                      // x = 1/x
  80.  
  81.   friend int       sign(const Rational& x);             // -1, 0, or +1
  82.   friend Rational  abs(const Rational& x);              // absolute value
  83.   friend Rational  sqr(const Rational& x);              // square
  84.   friend Rational  pow(const Rational& x, long y);
  85.   friend Rational  pow(const Rational& x, Integer& y);
  86.   const Integer&   numerator() const;
  87.   const Integer&   denominator() const;
  88.  
  89. // coercion & conversion
  90.  
  91.                    operator double() const;
  92.   friend Integer   floor(const Rational& x);
  93.   friend Integer   ceil(const Rational& x);
  94.   friend Integer   trunc(const Rational& x);
  95.   friend Integer   round(const Rational& x);
  96.  
  97.   friend istream&  operator >> (istream& s, Rational& y);
  98.   friend ostream&  operator << (ostream& s, const Rational& y);
  99.  
  100.  
  101. // procedural versions of operators
  102.  
  103.   friend int       compare(const Rational& x, const Rational& y);
  104.   friend void      add(const Rational& x, const Rational& y, Rational& dest);
  105.   friend void      sub(const Rational& x, const Rational& y, Rational& dest);
  106.   friend void      mul(const Rational& x, const Rational& y, Rational& dest);
  107.   friend void      div(const Rational& x, const Rational& y, Rational& dest);
  108.  
  109. // error detection
  110.  
  111.   volatile void    error(const char* msg) const;
  112.   int              OK() const;
  113.  
  114. };
  115.  
  116. typedef Rational RatTmp; // backwards compatibility
  117.  
  118. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  119.  
  120. inline Rational::Rational()  {}
  121. inline Rational::~Rational() {}
  122.  
  123. inline Rational::Rational(const Rational& y) :num(y.num), den(y.den) {}
  124.  
  125. inline Rational::Rational(const Integer& n) :num(n), den(1) {}
  126.  
  127. inline Rational::Rational(const Integer& n, const Integer& d) :num(n),den(d)
  128. {
  129.   normalize();
  130. }
  131.  
  132. inline Rational::Rational(long n, long d) :num(n), den(d)
  133. {
  134.   normalize();
  135. }
  136.  
  137. inline  void Rational::operator =  (const Rational& y)
  138. {
  139.   num = y.num;  den = y.den;
  140. }
  141.  
  142. inline int operator == (const Rational& x, const Rational& y)
  143. {
  144.   return compare(x.num, y.num) == 0 && compare(x.den, y.den) == 0;
  145. }
  146.  
  147. inline int operator != (const Rational& x, const Rational& y)
  148. {
  149.   return compare(x.num, y.num) != 0 || compare(x.den, y.den) != 0;
  150. }
  151.  
  152. inline int operator <  (const Rational& x, const Rational& y)
  153. {
  154.   return compare(x, y) <  0; 
  155. }
  156.  
  157. inline int operator <= (const Rational& x, const Rational& y)
  158. {
  159.   return compare(x, y) <= 0; 
  160. }
  161.  
  162. inline int operator >  (const Rational& x, const Rational& y)
  163. {
  164.   return compare(x, y) >  0; 
  165. }
  166.  
  167. inline int operator >= (const Rational& x, const Rational& y)
  168. {
  169.   return compare(x, y) >= 0; 
  170. }
  171.  
  172. inline int sign(const Rational& x)
  173. {
  174.   return sign(x.num);
  175. }
  176.  
  177. inline void Rational::negate()
  178. {
  179.   num.negate();
  180. }
  181.  
  182.  
  183. inline void Rational::operator += (const Rational& y) 
  184. {
  185.   add(*this, y, *this);
  186. }
  187.  
  188. inline void Rational::operator -= (const Rational& y) 
  189. {
  190.   sub(*this, y, *this);
  191. }
  192.  
  193. inline void Rational::operator *= (const Rational& y) 
  194. {
  195.   mul(*this, y, *this);
  196. }
  197.  
  198. inline void Rational::operator /= (const Rational& y) 
  199. {
  200.   div(*this, y, *this);
  201. }
  202.  
  203. inline const Integer& Rational::numerator() const { return num; }
  204. inline const Integer& Rational::denominator() const { return den; }
  205. inline Rational::operator double() const { return ratio(num, den); }
  206.  
  207. #ifdef __GNUG__
  208. inline Rational operator <? (const Rational& x, const Rational& y)
  209. {
  210.   if (compare(x, y) <= 0) return x; else return y;
  211. }
  212.  
  213. inline Rational operator >? (const Rational& x, const Rational& y)
  214. {
  215.   if (compare(x, y) >= 0) return x; else return y;
  216. }
  217. #endif
  218.  
  219. #if defined(__GNUG__) && !defined(NO_NRV)
  220.  
  221. inline Rational operator + (const Rational& x, const Rational& y) return r
  222. {
  223.   add(x, y, r);
  224. }
  225.  
  226. inline Rational operator - (const Rational& x, const Rational& y) return r
  227. {
  228.   sub(x, y, r);
  229. }
  230.  
  231. inline Rational operator * (const Rational& x, const Rational& y) return r
  232. {
  233.   mul(x, y, r);
  234. }
  235.  
  236. inline Rational operator / (const Rational& x, const Rational& y) return r
  237. {
  238.   div(x, y, r);
  239. }
  240.  
  241. #else /* NO_NRV */
  242.  
  243. inline Rational operator + (const Rational& x, const Rational& y) 
  244. {
  245.   Rational r; add(x, y, r); return r;
  246. }
  247.  
  248. inline Rational operator - (const Rational& x, const Rational& y)
  249. {
  250.   Rational r; sub(x, y, r); return r;
  251. }
  252.  
  253. inline Rational operator * (const Rational& x, const Rational& y)
  254. {
  255.   Rational r; mul(x, y, r); return r;
  256. }
  257.  
  258. inline Rational operator / (const Rational& x, const Rational& y)
  259. {
  260.   Rational r; div(x, y, r); return r;
  261. }
  262. #endif
  263. #endif
  264.  
  265. #endif
  266.