- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/include/Rational.h

Go to the documentation of this file.

    00001 /* $Revision: 1.2 $ $Date: 2006/09/09 06:22:54 $ $Author: dgl $ $Name:  $ $Id: _rational_8h-source.html,v 1.2 2006/09/09 06:22:54 dgl Exp $ */
    00002 #ifndef Rational_H
    00003 #define Rational_H
    00004
    00005 // The Musimat Tutorial © 2006 Gareth Loy
    00006 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00007 // and published exclusively by The MIT Press.
    00008 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00009 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00010 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00011 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00012 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00013 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00014 // The Musimat website:                 http://www.musimat.com/
    00015 // This program is released under the terms of the GNU General Public License
    00016 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00017
    00021
    00022 #include "Musimat.h"
    00023 #include <iostream>
    00024 using namespace std;
    00025
    00027
    00028 class Rational
    00029 {
    00030         friend ostream& operator<<(ostream&, Rational&);
    00031         friend istream& operator>>( istream& is, Rational& p );
    00032
    00033         friend Bool operator==(Integer& i, Rational& p) { Return i == p.quotient(); }
    00034         friend Bool operator==(Rational& p, Integer& i) { Return p.quotient() == i; }
    00035         friend Bool operator!=(Integer& i, Rational& p) { Return i != p.quotient(); }
    00036         friend Bool operator!=(Rational& p, Integer& i) { Return p.quotient() != i; }
    00037         friend Bool operator>=(Rational& p, Integer& r) { Return p.quotient() >= r; }
    00038         friend Bool operator>=(Integer& i, Rational& p) { Return i >= p.quotient(); }
    00039         friend Bool operator>(Rational& p, Integer& i) { Return p.quotient() > i; }
    00040         friend Bool operator>(Integer& i, Rational& p) { Return i > p.quotient(); }
    00041         friend Bool operator<=(Rational& p, Integer& i) { Return p.quotient() <= i; }
    00042         friend Bool operator<=(Integer& i, Rational& p) { Return i <= p.quotient(); }
    00043
    00044         friend Bool operator==(Real& r, Rational& p) { Return r == p.quotient(); }
    00045         friend Bool operator==(Rational& p, Real& r) { Return p.quotient() == r; }
    00046         friend Bool operator!=(Real& r, Rational& p) { Return r != p.quotient(); }
    00047         friend Bool operator!=(Rational& p, Real& r){ Return p.quotient() != r; }
    00048         friend Bool operator>=(Rational& p, Real& r) { Return p.quotient() >= r; }
    00049         friend Bool operator>=(Real& r, Rational& p) { Return r >= p.quotient(); }
    00050         friend Bool operator>(Rational& p, Real& r) { Return p.quotient() > r; }
    00051         friend Bool operator>(Real& r, Rational& p) { Return r > p.quotient(); }
    00052         friend Bool operator<=(Rational& p, Real& r) { Return p.quotient() <= r; }
    00053         friend Bool operator<=(Real& r, Rational& p) { Return r <= p.quotient(); }
    00054
    00055 public:
    00056
    00058         Rational() : m_num(0), m_den(0) { }
    00059
    00061         Rational(Integer i) : m_num(i), m_den(i) { }
    00062
    00064         Rational(Const Rational& x) : m_num(x.m_num), m_den(x.m_den) { }
    00065
    00068         Rational(Real x) : m_num(0), m_den(0) { RealToRational( x, m_num, m_den ); }
    00069
    00073         Rational(Integer num, Integer den) : m_num(num), m_den(den) {
    00074                 If (m_den == 0)
    00075                         m_den = 1;
    00076         }
    00077
    00082         Rational(Real x, Integer& num, Integer& den) : m_num(0), m_den(0) {
    00083                 RealToRational( x, num, den );
    00084                 m_num = num;
    00085                 m_den = den;
    00086         }
    00087
    00092         Rational(Rational x, Integer& num, Integer& den) : m_num(0), m_den(0) {
    00093                 RealToRational( x.quotient(), num, den );
    00094                 m_num = num;
    00095                 m_den = den;
    00096         }
    00097
    00099         Rational& operator=(Const Rational& x) {
    00100                 m_num = x.m_num;
    00101                 m_den = x.m_den;
    00102                 Return *this;
    00103         }
    00104
    00106         Bool operator==(Const Rational& x) Const {
    00107                 Return m_num == x.m_num  And m_den == x.m_den;
    00108         }
    00109
    00111         Bool operator!=(Const Rational& x) Const {
    00112                 Return m_num != x.m_num || m_den != x.m_den;
    00113         }
    00114
    00116         Bool operator>=(Const Rational& x) Const {
    00117                 Return quotient() >= x.quotient();
    00118         }
    00119
    00121         Bool operator>(Const Rational& x) Const {
    00122                 Return quotient() > x.quotient();
    00123         }
    00124
    00126         Bool operator<(Const Rational& x) Const {
    00127                 Return quotient() < x.quotient();
    00128         }
    00129
    00131         Bool operator<=(Const Rational& x) Const {
    00132                 Return quotient() <= x.quotient();
    00133         }
    00134
    00136         Rational& operator+=(Const Real x) {
    00137                 Return( *this = Rational(quotient() + x) );
    00138         }
    00139
    00141         Rational& operator+=(Rational x) {
    00142                 Return( *this = Rational(quotient() + x.quotient() ) );
    00143         }
    00144
    00146         Rational& operator-=(Const Real x) {
    00147                 Return( *this = quotient() - x );
    00148         }
    00149
    00151         Rational& operator-=(Rational x) {
    00152                 Return( *this = quotient() - x.quotient() );
    00153         }
    00154
    00156         Rational& operator*=(Const Real x) {
    00157                 Return( *this = quotient() * x );
    00158         }
    00159
    00161         Rational& operator*=(Rational x) {
    00162                 Return( *this = quotient() * x.quotient() );
    00163         }
    00164
    00166         Rational& operator/=(Const Real x) {
    00167                 Return( *this = quotient() / x );
    00168         }
    00169
    00171         Rational& operator/=(Rational x) {
    00172                 Return( *this = quotient() / x.quotient() );
    00173         }
    00174
    00176         Rational operator+(Rational& x) {
    00177                 Return( Rational(quotient() + x.quotient()) );
    00178         }
    00179
    00181         Rational operator-(Rational& x) {
    00182                 Return( Rational(quotient() - x.quotient()) );
    00183         }
    00184
    00186         Rational operator*(Rational& x) {
    00187                 Return( Rational(quotient() * x.quotient()) );
    00188         }
    00189
    00191         Rational operator/(Rational& x) {
    00192                 Return( Rational(quotient() / x.quotient()) );
    00193         }
    00194
    00196         Rational operator+(Const Real x) {
    00197                 Return( Rational(quotient() + x) );
    00198         }
    00199
    00201         Rational operator-(Const Real x) {
    00202                 Return( Rational(quotient() - x) );
    00203         }
    00204
    00206         Rational operator*(Const Real x) {
    00207                 Return( Rational(quotient() * x) );
    00208         }
    00209
    00211         Rational operator/(Const Real x) {
    00212                 Return( Rational(quotient() / x ) );
    00213         }
    00214
    00217         String print(String s = 0) Const {
    00218                 If (s)
    00219                         cout << s;
    00220                 sprintf(s_buf, "{%d,%d}", m_num, m_den);
    00221                 Return s_buf;
    00222         }
    00223
    00224 private:
    00225         // Absolute value of a Real
    00226 //      Real abs( Real x ) Const { Return x < 0 ? -x : x; }
    00227         Real quotient() Const { Return( Real(m_num) / Real(m_den) ); }
    00228
    00229 protected:
    00230         Integer m_num;
    00231         Integer m_den;
    00232         Static char s_buf[128];
    00233 };
    00234
    00235 #endif // Rational_H