- Main Page
- Classes
- Files

# rational.hpp File Reference

`#include <istream>`
`#include <limits>`
`#include <ostream>`
`#include <sstream>`
`#include <stdexcept>`
`#include <string>`
`#include "gcd.hpp"`
`#include "ioflags.hpp"`

Go to the source code of this file.

## Classes
class | rational< T >
| Represent a rational number (fraction) as a numerator and denominator. More...
class | rational< T >::zero_denominator
| Exception class if the denominator is ever zero. More...
## Functions
template<class T>
rational< T > | operator- (rational< T > const &r)
| Negate a rational number.
template<class T>
rational< T > | absval (rational< T > const &r)
template<class T>
rational< T > | operator+ (rational< T > lhs, rational< T > const &rhs)
| Addition.
template<class T>
rational< T > | operator+ (rational< T > lhs, T const &rhs)
| Addition.
template<class T>
rational< T > | operator+ (T const &lhs, rational< T > rhs)
| Addition.
template<class T>
rational< T > | operator- (rational< T > lhs, rational< T > const &rhs)
| Subtraction.
template<class T>
rational< T > | operator- (rational< T > lhs, T const &rhs)
| Subtraction.
template<class T>
rational< T > | operator- (T const &lhs, rational< T > rhs)
| Subtraction.
template<class T>
rational< T > | operator * (rational< T > lhs, rational< T > const &rhs)
| Multiplication.
template<class T>
rational< T > | operator * (rational< T > lhs, T const &rhs)
| Multiplication.
template<class T>
rational< T > | operator * (T const &lhs, rational< T > rhs)
| Multiplication.
template<class T>
rational< T > | operator/ (rational< T > lhs, rational< T > const &rhs)
| Division.
template<class T>
rational< T > | operator/ (rational< T > lhs, T const &rhs)
| Division.
template<class T>
rational< T > | operator/ (T const &lhs, rational< T > rhs)
| Division.
template<class T, class U>
bool | operator== (rational< T > const &a, rational< U > const &b)
| Equality comparison.
template<class T>
bool | operator== (rational< T > const &lhs, T rhs)
| Equality comparison.
template<class T>
bool | operator== (T lhs, rational< T > const &rhs)
| Equality comparison.
template<class T>
bool | operator< (rational< T > const &a, rational< T > const &b)
| Less-than comparison.
template<class T>
bool | operator< (rational< T > const &a, T const &b)
| Less-than comparison.
template<class T>
bool | operator< (T const &a, rational< T > const &b)
| Less-than comparison.
template<class T, class U>
bool | operator!= (rational< T > const &a, rational< U > const &b)
| Inequality comparison.
template<class T>
bool | operator!= (rational< T > const &a, T b)
| Inequality comparison.
template<class T>
bool | operator!= (T a, rational< T > const &b)
| Inequality comparison.
template<class T>
bool | operator<= (rational< T > const &a, rational< T > const &b)
| Less-than-or-equal comparison.
template<class T>
bool | operator<= (rational< T > const &a, T const &b)
| Less-than-or-equal comparison.
template<class T>
bool | operator<= (T const &a, rational< T > const &b)
| Less-than-or-equal comparison.
template<class T>
bool | operator> (rational< T > const &a, rational< T > const &b)
| Greater-than comparison.
template<class T>
bool | operator> (rational< T > const &a, T const &b)
| Greater-than comparison.
template<class T>
bool | operator> (T const &a, rational< T > const &b)
| Greater-than comparison.
template<class T>
bool | operator>= (rational< T > const &a, rational< T > const &b)
| Greater-than-or-equal comparison.
template<class T>
bool | operator>= (rational< T > const &a, T const &b)
| Greater-than-or-equal comparison.
template<class T>
bool | operator>= (T const &a, rational< T > const &b)
| Greater-than-or-equal comparison.
template<class T, class Char, class Traits>
std::basic_istream < Char, Traits > & | operator>> (std::basic_istream< Char, Traits > &in, rational< T > &rat)
| Input operator.
template<class T, class Char, class Traits>
std::basic_ostream < Char, Traits > & | operator<< (std::basic_ostream< Char, Traits > &out, rational< T > const &rat)
| Output operator.

## Function Documentation

template<class T>

rational<T> absval | ( | rational< T > const & | r | ) | `[inline]`

Definition at line 285 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

    00286 {
    00287   using namespace std;
    00288   return rational<T>(abs(r.numerator()), r.denominator());
    00289 }

template<class T>

rational<T> operator * | ( | T const & | lhs,
|  | rational< T > | rhs |
| ) |  |  | `[inline]`

Multiplication.

Definition at line 358 of file rational.hpp.

    00359 {
    00360   rhs *= lhs;
    00361   return rhs;
    00362 }

template<class T>

rational<T> operator * | ( | rational< T > | lhs,
|  | T const & | rhs |
| ) |  |  | `[inline]`

Multiplication.

Definition at line 350 of file rational.hpp.

    00351 {
    00352   lhs *= rhs;
    00353   return lhs;
    00354 }

template<class T>

rational<T> operator * | ( | rational< T > | lhs,
|  | rational< T > const & | rhs |
| ) |  |  | `[inline]`

Multiplication.

Definition at line 342 of file rational.hpp.

    00343 {
    00344   lhs *= rhs;
    00345   return lhs;
    00346 }

template<class T>

bool operator!= | ( | T | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Inequality comparison.

Definition at line 449 of file rational.hpp.

    00450 {
    00451   return not (a == b);
    00452 }

template<class T>

bool operator!= | ( | rational< T > const & | a,
|  | T | b |
| ) |  |  | `[inline]`

Inequality comparison.

Definition at line 442 of file rational.hpp.

    00443 {
    00444   return not (a == b);
    00445 }

template<class T, class U>

bool operator!= | ( | rational< T > const & | a,
|  | rational< U > const & | b |
| ) |  |  | `[inline]`

Inequality comparison.

Definition at line 435 of file rational.hpp.

    00436 {
    00437   return not (a == b);
    00438 }

template<class T>

rational<T> operator+ | ( | T const & | lhs,
|  | rational< T > | rhs |
| ) |  |  | `[inline]`

Addition.

Definition at line 309 of file rational.hpp.

    00310 {
    00311   rhs += lhs;
    00312   return rhs;
    00313 }

template<class T>

rational<T> operator+ | ( | rational< T > | lhs,
|  | T const & | rhs |
| ) |  |  | `[inline]`

Addition.

Definition at line 301 of file rational.hpp.

    00302 {
    00303   lhs += rhs;
    00304   return lhs;
    00305 }

template<class T>

rational<T> operator+ | ( | rational< T > | lhs,
|  | rational< T > const & | rhs |
| ) |  |  | `[inline]`

Addition.

Definition at line 293 of file rational.hpp.

    00294 {
    00295   lhs += rhs;
    00296   return lhs;
    00297 }

template<class T>

rational<T> operator- | ( | T const & | lhs,
|  | rational< T > | rhs |
| ) |  |  | `[inline]`

Subtraction.

Definition at line 333 of file rational.hpp.

    00334 {
    00335   // Gotta be a little tricky.
    00336   rhs += -lhs;
    00337   return -rhs;
    00338 }

template<class T>

rational<T> operator- | ( | rational< T > | lhs,
|  | T const & | rhs |
| ) |  |  | `[inline]`

Subtraction.

Definition at line 325 of file rational.hpp.

    00326 {
    00327   lhs -= rhs;
    00328   return lhs;
    00329 }

template<class T>

rational<T> operator- | ( | rational< T > | lhs,
|  | rational< T > const & | rhs |
| ) |  |  | `[inline]`

Subtraction.

Definition at line 317 of file rational.hpp.

    00318 {
    00319   lhs -= rhs;
    00320   return lhs;
    00321 }

template<class T>

rational<T> operator- | ( | rational< T > const & | r | ) | `[inline]`

Negate a rational number.

Definition at line 279 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

    00280 {
    00281   return rational<T>(-r.numerator(), r.denominator());
    00282 }

template<class T>

rational<T> operator/ | ( | T const & | lhs,
|  | rational< T > | rhs |
| ) |  |  | `[inline]`

Division.

Definition at line 382 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

    00383 {
    00384   return rational<T>(lhs * rhs.denominator(), rhs.numerator());
    00385 }

template<class T>

rational<T> operator/ | ( | rational< T > | lhs,
|  | T const & | rhs |
| ) |  |  | `[inline]`

Division.

Definition at line 374 of file rational.hpp.

    00375 {
    00376   lhs /= rhs;
    00377   return lhs;
    00378 }

template<class T>

rational<T> operator/ | ( | rational< T > | lhs,
|  | rational< T > const & | rhs |
| ) |  |  | `[inline]`

Division.

Definition at line 366 of file rational.hpp.

    00367 {
    00368   lhs /= rhs;
    00369   return lhs;
    00370 }

template<class T>

bool operator< | ( | T const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Less-than comparison.

Definition at line 428 of file rational.hpp.

    00429 {
    00430   return a * b.denominator() < b.numerator();
    00431 }

template<class T>

bool operator< | ( | rational< T > const & | a,
|  | T const & | b |
| ) |  |  | `[inline]`

Less-than comparison.

Definition at line 421 of file rational.hpp.

    00422 {
    00423   return a.numerator() < b * a.denominator();
    00424 }

template<class T>

bool operator< | ( | rational< T > const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Less-than comparison.

Definition at line 414 of file rational.hpp.

    00415 {
    00416   return a.numerator() * b.denominator() < b.numerator() * a.denominator();
    00417 }

template<class T, class Char, class Traits>

std::basic_ostream<Char, Traits>& operator<< | ( | std::basic_ostream< Char, Traits > & | out,
|  | rational< T > const & | rat |
| ) |  |  | `[inline]`

Output operator.

Definition at line 555 of file rational.hpp.

    00556 {
    00557   typename std::basic_ostream<Char, Traits>::sentry sentry(out);
    00558   std::ostringstream stream;
    00559   stream << rat.numerator() << '/' << rat.denominator();
    00560   out << stream.str();
    00561   return out;
    00562 }

template<class T>

bool operator<= | ( | T const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Less-than-or-equal comparison.

Definition at line 470 of file rational.hpp.

    00471 {
    00472   return not (b < a);
    00473 }

template<class T>

bool operator<= | ( | rational< T > const & | a,
|  | T const & | b |
| ) |  |  | `[inline]`

Less-than-or-equal comparison.

Definition at line 463 of file rational.hpp.

    00464 {
    00465   return not (b < a);
    00466 }

template<class T>

bool operator<= | ( | rational< T > const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Less-than-or-equal comparison.

Definition at line 456 of file rational.hpp.

    00457 {
    00458   return not (b < a);
    00459 }

template<class T>

bool operator== | ( | T | lhs,
|  | rational< T > const & | rhs |
| ) |  |  | `[inline]`

Equality comparison.

Definition at line 406 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

    00407 {
    00408   return rhs.denominator() == 1 and
    00409          rhs.numerator()   == lhs;
    00410 }

template<class T>

bool operator== | ( | rational< T > const & | lhs,
|  | T | rhs |
| ) |  |  | `[inline]`

Equality comparison.

Definition at line 398 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

    00399 {
    00400   return lhs.denominator() == 1 and
    00401          lhs.numerator()   == rhs;
    00402 }

template<class T, class U>

bool operator== | ( | rational< T > const & | a,
|  | rational< U > const & | b |
| ) |  |  | `[inline]`

Equality comparison.

Definition at line 390 of file rational.hpp.

References rational< T >::denominator(), and rational< T >::numerator().

    00391 {
    00392   return a.numerator() == b.numerator() and
    00393          a.denominator() == b.denominator();
    00394 }

template<class T>

bool operator> | ( | T const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Greater-than comparison.

Definition at line 491 of file rational.hpp.

    00492 {
    00493   return b < a;
    00494 }

template<class T>

bool operator> | ( | rational< T > const & | a,
|  | T const & | b |
| ) |  |  | `[inline]`

Greater-than comparison.

Definition at line 484 of file rational.hpp.

    00485 {
    00486   return b < a;
    00487 }

template<class T>

bool operator> | ( | rational< T > const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Greater-than comparison.

Definition at line 477 of file rational.hpp.

    00478 {
    00479   return b < a;
    00480 }

template<class T>

bool operator>= | ( | T const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Greater-than-or-equal comparison.

Definition at line 512 of file rational.hpp.

    00513 {
    00514   return not (b > a);
    00515 }

template<class T>

bool operator>= | ( | rational< T > const & | a,
|  | T const & | b |
| ) |  |  | `[inline]`

Greater-than-or-equal comparison.

Definition at line 505 of file rational.hpp.

    00506 {
    00507   return not (b > a);
    00508 }

template<class T>

bool operator>= | ( | rational< T > const & | a,
|  | rational< T > const & | b |
| ) |  |  | `[inline]`

Greater-than-or-equal comparison.

Definition at line 498 of file rational.hpp.

    00499 {
    00500   return not (b > a);
    00501 }

template<class T, class Char, class Traits>

std::basic_istream<Char, Traits>& operator>> | ( | std::basic_istream< Char, Traits > & | in,
|  | rational< T > & | rat |
| ) |  |  | `[inline]`

Input operator.

Definition at line 519 of file rational.hpp.

    00520 {
    00521   typename std::basic_istream<Char, Traits>::sentry sentry(in, false);
    00522   ioflags flags(in);
    00523
    00524   T n = T();
    00525   if (not (in >> n))
    00526     // Error reading the numerator.
    00527     return in;
    00528
    00529   in >> std::noskipws;
    00530   char sep('\0');
    00531   if (not (in >> sep))
    00532     // Error reading the separator character.
    00533     return in;
    00534   else if (sep != '/')
    00535   {
    00536     // Push sep back into the input stream, so the next input operation
    00537     // will read it.
    00538     in.unget();
    00539     rat = n;
    00540     return in;
    00541   }
    00542   else
    00543   {
    00544     T d = T();
    00545     if (in >> d)
    00546       // Successfully read numerator, separator, and denominator.
    00547       rat = rational<T>(n, d);
    00548   }
    00549
    00550   return in;
    00551 }