- Main Page
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Members

# Rational Class Reference

Rational class implements standard methods and data structures to manipulate Rational ratios arithmetically. More...

`#include <Rational.h>`

List of all members.

## Public Member Functions
| Rational ()
| Default constructor.
| Rational (Integer i)
| Default constructor.
| Rational (Const Rational &x)
| Copy constructor.
| Rational (Real x)
| Construct Rational given a fractional value.
| Rational (Integer num, Integer den)
| Construct Rational given a rational fraction value.
| Rational (Real x, Integer &num, Integer &den)
| Construct Rational given a Real fractional value, determine its rational approximation, assign numerator and denominator to reference arguments.
| Rational (Rational x, Integer &num, Integer &den)
| Construct Rational given a Rational, determine its rational approximation, assign numerator and denominator to reference arguments.
Rational & | operator= (Const Rational &x)
| Rational assignment operator.
Bool | operator== (Const Rational &x) Const
| Rational equality operator.
Bool | operator!= (Const Rational &x) Const
| Rational inequality operator.
Bool | operator>= (Const Rational &x) Const
| Rational >= operator.
Bool | operator> (Const Rational &x) Const
| Rational > operator.
Bool | operator< (Const Rational &x) Const
| Rational < operator.
Bool | operator<= (Const Rational &x) Const
| Rational <= operator.
Rational & | operator+= (Const Real x)
| Rational plus-equals operator.
Rational & | operator+= (Rational x)
| Rational plus-equals operator.
Rational & | operator-= (Const Real x)
| Rational minus-equals operator.
Rational & | operator-= (Rational x)
| Rational minus-equals operator.
Rational & | operator *= (Const Real x)
| Rational times-equals operator.
Rational & | operator *= (Rational x)
| Rational times-equals operator.
Rational & | operator/= (Const Real x)
| Rational divide-equals operator.
Rational & | operator/= (Rational x)
| Rational divide-equals operator.
Rational | operator+ (Rational &x)
| Rational addition operator.
Rational | operator- (Rational &x)
| Rational subtraction operator.
Rational | operator * (Rational &x)
| Rational times operator.
Rational | operator/ (Rational &x)
| Rational divide operator.
Rational | operator+ (Const Real x)
| Rational add to Real operator.
Rational | operator- (Const Real x)
| Rational subtract from Real operator.
Rational | operator * (Const Real x)
| Rational multiply by Real operator.
Rational | operator/ (Const Real x)
| Rational divide by Real operator.
String | print (String s=0) Const
| Print Rational prefixed by String.
## Protected Attributes
Integer | m_num
Integer | m_den
Static char | s_buf [128]
## Friends
ostream & | operator<< (ostream &, Rational &)
istream & | operator>> (istream &is, Rational &p)
Bool | operator== (Integer &i, Rational &p)
Bool | operator== (Rational &p, Integer &i)
Bool | operator!= (Integer &i, Rational &p)
Bool | operator!= (Rational &p, Integer &i)
Bool | operator>= (Rational &p, Integer &r)
Bool | operator>= (Integer &i, Rational &p)
Bool | operator> (Rational &p, Integer &i)
Bool | operator> (Integer &i, Rational &p)
Bool | operator<= (Rational &p, Integer &i)
Bool | operator<= (Integer &i, Rational &p)
Bool | operator== (Real &r, Rational &p)
Bool | operator== (Rational &p, Real &r)
Bool | operator!= (Real &r, Rational &p)
Bool | operator!= (Rational &p, Real &r)
Bool | operator>= (Rational &p, Real &r)
Bool | operator>= (Real &r, Rational &p)
Bool | operator> (Rational &p, Real &r)
Bool | operator> (Real &r, Rational &p)
Bool | operator<= (Rational &p, Real &r)
Bool | operator<= (Real &r, Rational &p)

## Detailed Description

Rational class implements standard methods and data structures to manipulate Rational ratios arithmetically.

Definition at line 28 of file Rational.h.

## Constructor & Destructor Documentation

Rational::Rational | ( |  | ) | `[inline]`

Default constructor.

Definition at line 58 of file Rational.h.

Referenced by operator *(), operator+(), operator+=(), operator-(), and operator/().

    00058 : m_num(0), m_den(0) { }

Rational::Rational | ( | Integer | i | ) | `[inline]`

Default constructor.

Definition at line 61 of file Rational.h.

    00061 : m_num(i), m_den(i) { }

Rational::Rational | ( | Const Rational & | x | ) | `[inline]`

Copy constructor.

Definition at line 64 of file Rational.h.

    00064 : m_num(x.m_num), m_den(x.m_den) { }

Rational::Rational | ( | Real | x | ) | `[inline]`

Construct Rational given a fractional value.

Parameters:

  | x | Rational fraction expressed as a Real

Definition at line 68 of file Rational.h.

References m_den, m_num, and RealToRational().

    00068 : m_num(0), m_den(0) { RealToRational( x, m_num, m_den ); }

Rational::Rational | ( | Integer | num,
|  | Integer | den |
| ) |  |  | `[inline]`

Construct Rational given a rational fraction value.

Parameters:

  | num | Numerator of rational fraction
  | den | Denominator of rational fraction

Definition at line 73 of file Rational.h.

References If, and m_den.

    00073                                            : m_num(num), m_den(den) {
    00074                 If (m_den == 0)
    00075                         m_den = 1;
    00076         }

Rational::Rational | ( | Real | x,
|  | Integer & | num,
|  | Integer & | den |
| ) |  |  | `[inline]`

Construct Rational given a Real fractional value, determine its rational approximation, assign numerator and denominator to reference arguments.

Parameters:

  | x | Real fractional reference value
  | num | Reference Integer numerator of derived rational fraction approximation
  | den | Reference Integer denominator of derived rational fraction approximation

Definition at line 82 of file Rational.h.

References m_den, m_num, and RealToRational().

    00082                                                      : m_num(0), m_den(0) {
    00083                 RealToRational( x, num, den );
    00084                 m_num = num;
    00085                 m_den = den;
    00086         }

Rational::Rational | ( | Rational | x,
|  | Integer & | num,
|  | Integer & | den |
| ) |  |  | `[inline]`

Construct Rational given a Rational, determine its rational approximation, assign numerator and denominator to reference arguments.

Parameters:

  | x | Rational value
  | num | Reference Integer numerator of derived rational fraction approximation
  | den | Reference Integer denominator of derived rational fraction approximation

Definition at line 92 of file Rational.h.

References m_den, m_num, quotient(), and RealToRational().

    00092                                                          : m_num(0), m_den(0) {
    00093                 RealToRational( x.quotient(), num, den );
    00094                 m_num = num;
    00095                 m_den = den;
    00096         }

## Member Function Documentation

Rational Rational::operator * | ( | Const Real | x | ) | `[inline]`

Rational multiply by Real operator.

Definition at line 206 of file Rational.h.

References Rational(), and Return.

    00206                                          {
    00207                 Return( Rational(quotient() * x) );
    00208         }

Rational Rational::operator * | ( | Rational & | x | ) | `[inline]`

Rational times operator.

Definition at line 186 of file Rational.h.

References quotient(), Rational(), and Return.

    00186                                         {
    00187                 Return( Rational(quotient() * x.quotient()) );
    00188         }

Rational& Rational::operator *= | ( | Rational | x | ) | `[inline]`

Rational times-equals operator.

Definition at line 161 of file Rational.h.

References quotient(), and Return.

    00161                                          {
    00162                 Return( *this = quotient() * x.quotient() );
    00163         }

Rational& Rational::operator *= | ( | Const Real | x | ) | `[inline]`

Rational times-equals operator.

Definition at line 156 of file Rational.h.

References Return.

    00156                                            {
    00157                 Return( *this = quotient() * x );
    00158         }

Bool Rational::operator!= | ( | Const Rational & | x | ) | `[inline]`

Rational inequality operator.

Definition at line 111 of file Rational.h.

References m_den, m_num, and Return.

    00111                                                  {
    00112                 Return m_num != x.m_num || m_den != x.m_den;
    00113         }

Rational Rational::operator+ | ( | Const Real | x | ) | `[inline]`

Rational add to Real operator.

Definition at line 196 of file Rational.h.

References Rational(), and Return.

    00196                                          {
    00197                 Return( Rational(quotient() + x) );
    00198         }

Rational Rational::operator+ | ( | Rational & | x | ) | `[inline]`

Rational addition operator.

Definition at line 176 of file Rational.h.

References quotient(), Rational(), and Return.

    00176                                         {
    00177                 Return( Rational(quotient() + x.quotient()) );
    00178         }

Rational& Rational::operator+= | ( | Rational | x | ) | `[inline]`

Rational plus-equals operator.

Definition at line 141 of file Rational.h.

References quotient(), Rational(), and Return.

    00141                                          {
    00142                 Return( *this = Rational(quotient() + x.quotient() ) );
    00143         }

Rational& Rational::operator+= | ( | Const Real | x | ) | `[inline]`

Rational plus-equals operator.

Definition at line 136 of file Rational.h.

References Rational(), and Return.

    00136                                            {
    00137                 Return( *this = Rational(quotient() + x) );
    00138         }

Rational Rational::operator- | ( | Const Real | x | ) | `[inline]`

Rational subtract from Real operator.

Definition at line 201 of file Rational.h.

References Rational(), and Return.

    00201                                          {
    00202                 Return( Rational(quotient() - x) );
    00203         }

Rational Rational::operator- | ( | Rational & | x | ) | `[inline]`

Rational subtraction operator.

Definition at line 181 of file Rational.h.

References quotient(), Rational(), and Return.

    00181                                         {
    00182                 Return( Rational(quotient() - x.quotient()) );
    00183         }

Rational& Rational::operator-= | ( | Rational | x | ) | `[inline]`

Rational minus-equals operator.

Definition at line 151 of file Rational.h.

References quotient(), and Return.

    00151                                          {
    00152                 Return( *this = quotient() - x.quotient() );
    00153         }

Rational& Rational::operator-= | ( | Const Real | x | ) | `[inline]`

Rational minus-equals operator.

Definition at line 146 of file Rational.h.

References Return.

    00146                                            {
    00147                 Return( *this = quotient() - x );
    00148         }

Rational Rational::operator/ | ( | Const Real | x | ) | `[inline]`

Rational divide by Real operator.

Definition at line 211 of file Rational.h.

References Rational(), and Return.

    00211                                          {
    00212                 Return( Rational(quotient() / x ) );
    00213         }

Rational Rational::operator/ | ( | Rational & | x | ) | `[inline]`

Rational divide operator.

Definition at line 191 of file Rational.h.

References quotient(), Rational(), and Return.

    00191                                         {
    00192                 Return( Rational(quotient() / x.quotient()) );
    00193         }

Rational& Rational::operator/= | ( | Rational | x | ) | `[inline]`

Rational divide-equals operator.

Definition at line 171 of file Rational.h.

References quotient(), and Return.

    00171                                          {
    00172                 Return( *this = quotient() / x.quotient() );
    00173         }

Rational& Rational::operator/= | ( | Const Real | x | ) | `[inline]`

Rational divide-equals operator.

Definition at line 166 of file Rational.h.

References Return.

    00166                                            {
    00167                 Return( *this = quotient() / x );
    00168         }

Bool Rational::operator< | ( | Const Rational & | x | ) | `[inline]`

Rational < operator.

Definition at line 126 of file Rational.h.

References Return.

    00126                                                 {
    00127                 Return quotient() < x.quotient();
    00128         }

Bool Rational::operator<= | ( | Const Rational & | x | ) | `[inline]`

Rational <= operator.

Definition at line 131 of file Rational.h.

References Return.

    00131                                                  {
    00132                 Return quotient() <= x.quotient();
    00133         }

Rational& Rational::operator= | ( | Const Rational & | x | ) | `[inline]`

Rational assignment operator.

Definition at line 99 of file Rational.h.

References m_den, m_num, and Return.

    00099                                                {
    00100                 m_num = x.m_num;
    00101                 m_den = x.m_den;
    00102                 Return *this;
    00103         }

Bool Rational::operator== | ( | Const Rational & | x | ) | `[inline]`

Rational equality operator.

Definition at line 106 of file Rational.h.

References And, m_den, m_num, and Return.

    00106                                                  {
    00107                 Return m_num == x.m_num  And m_den == x.m_den;
    00108         }

Bool Rational::operator> | ( | Const Rational & | x | ) | `[inline]`

Rational > operator.

Definition at line 121 of file Rational.h.

References Return.

    00121                                                 {
    00122                 Return quotient() > x.quotient();
    00123         }

Bool Rational::operator>= | ( | Const Rational & | x | ) | `[inline]`

Rational >= operator.

Definition at line 116 of file Rational.h.

References Return.

    00116                                                  {
    00117                 Return quotient() >= x.quotient();
    00118         }

String Rational::print | ( | String | s = `0` | ) | `[inline]`

Print Rational prefixed by String.

Parameters:

  | s | Prefix string

Definition at line 217 of file Rational.h.

References If, m_den, m_num, Return, and s_buf.

Referenced by operator<<().

    00217                                          {
    00218                 If (s)
    00219                         cout << s;
    00220                 sprintf(s_buf, "{%d,%d}", m_num, m_den);
    00221                 Return s_buf;
    00222         }

## Friends And Related Function Documentation

Bool operator!= | ( | Rational & | p,
|  | Real & | r |
| ) |  |  | `[friend]`

Definition at line 47 of file Rational.h.

    00047 { Return p.quotient() != r; }

Bool operator!= | ( | Real & | r,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 46 of file Rational.h.

    00046 { Return r != p.quotient(); }

Bool operator!= | ( | Rational & | p,
|  | Integer & | i |
| ) |  |  | `[friend]`

Definition at line 36 of file Rational.h.

    00036 { Return p.quotient() != i; }

Bool operator!= | ( | Integer & | i,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 35 of file Rational.h.

    00035 { Return i != p.quotient(); }

ostream& operator<< | ( | ostream & | os,
|  | Rational & | r |
| ) |  |  | `[friend]`

Definition at line 21 of file Rational.cpp.

    00022 {
    00023         os << r.print();
    00024         Return os;
    00025 }

Bool operator<= | ( | Real & | r,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 53 of file Rational.h.

    00053 { Return r <= p.quotient(); }

Bool operator<= | ( | Rational & | p,
|  | Real & | r |
| ) |  |  | `[friend]`

Definition at line 52 of file Rational.h.

    00052 { Return p.quotient() <= r; }

Bool operator<= | ( | Integer & | i,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 42 of file Rational.h.

    00042 { Return i <= p.quotient(); }

Bool operator<= | ( | Rational & | p,
|  | Integer & | i |
| ) |  |  | `[friend]`

Definition at line 41 of file Rational.h.

    00041 { Return p.quotient() <= i; }

Bool operator== | ( | Rational & | p,
|  | Real & | r |
| ) |  |  | `[friend]`

Definition at line 45 of file Rational.h.

    00045 { Return p.quotient() == r; }

Bool operator== | ( | Real & | r,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 44 of file Rational.h.

    00044 { Return r == p.quotient(); }

Bool operator== | ( | Rational & | p,
|  | Integer & | i |
| ) |  |  | `[friend]`

Definition at line 34 of file Rational.h.

    00034 { Return p.quotient() == i; }

Bool operator== | ( | Integer & | i,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 33 of file Rational.h.

    00033 { Return i == p.quotient(); }

Bool operator> | ( | Real & | r,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 51 of file Rational.h.

    00051 { Return r > p.quotient(); }

Bool operator> | ( | Rational & | p,
|  | Real & | r |
| ) |  |  | `[friend]`

Definition at line 50 of file Rational.h.

    00050 { Return p.quotient() > r; }

Bool operator> | ( | Integer & | i,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 40 of file Rational.h.

    00040 { Return i > p.quotient(); }

Bool operator> | ( | Rational & | p,
|  | Integer & | i |
| ) |  |  | `[friend]`

Definition at line 39 of file Rational.h.

    00039 { Return p.quotient() > i; }

Bool operator>= | ( | Real & | r,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 49 of file Rational.h.

    00049 { Return r >= p.quotient(); }

Bool operator>= | ( | Rational & | p,
|  | Real & | r |
| ) |  |  | `[friend]`

Definition at line 48 of file Rational.h.

    00048 { Return p.quotient() >= r; }

Bool operator>= | ( | Integer & | i,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 38 of file Rational.h.

    00038 { Return i >= p.quotient(); }

Bool operator>= | ( | Rational & | p,
|  | Integer & | r |
| ) |  |  | `[friend]`

Definition at line 37 of file Rational.h.

    00037 { Return p.quotient() >= r; }

istream& operator>> | ( | istream & | is,
|  | Rational & | p |
| ) |  |  | `[friend]`

Definition at line 27 of file Rational.cpp.

    00028 {
    00029         char ch;
    00030         is >> ch && (ch == '{' || ch == '(')
    00031                 && is >> p.m_num >> ch && ch == ','
    00032                 && is >> p.m_den >> ch && (ch == '}' || ch == '(');
    00033         Return is;
    00034 }

## Member Data Documentation

Integer Rational::m_den`[protected]`

Definition at line 231 of file Rational.h.

Referenced by operator!=(), operator=(), operator==(), operator>>(), print(), and Rational().

Integer Rational::m_num`[protected]`

Definition at line 230 of file Rational.h.

Referenced by operator!=(), operator=(), operator==(), operator>>(), print(), and Rational().

Character Rational::s_buf`[protected]`

Definition at line 232 of file Rational.h.

Referenced by print().

The documentation for this class was generated from the following files:

- C:/Musimathics_local/Musimat/include/Rational.h
- C:/Musimathics_local/Musimat/MusimatLib/Rational.cpp