- Main Page
- Classes
- Files

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# rational< T > Class Template Reference

Represent a rational number (fraction) as a numerator and denominator. More...

`#include <rational.hpp>`

List of all members.

## Public Types
typedef T | value_type
| Convenience typedef for the integral type of the numerator and denominator.
## Public Member Functions
| rational (value_type num=0)
| rational (value_type num, value_type den)
| rational (double r)
template<class U>
| rational (rational< U > const &that)
| Copy from a different type of rational.
value_type | numerator () const
| Return the numerator.
value_type | denominator () const
| Return the denominator.
template<class U>
U | as () const
| Convert the rational number to another type, especially floating-point.
rational & | operator= (value_type)
| Assignment of an integer.
template<class U>
rational & | operator= (rational< U > const &rhs)
| Assignment of a rational with a different size.
rational & | operator+= (rational const &rhs)
| Addition assignment operator.
rational & | operator+= (value_type const &rhs)
| Addition assignment operator.
rational & | operator-= (rational const &rhs)
| Subtraction assignment operator.
rational & | operator-= (value_type const &rhs)
| Subtraction assignment operator.
rational & | operator *= (rational const &rhs)
| Multiplication assignment operator.
rational & | operator *= (value_type const &rhs)
| Multiplication assignment operator.
rational & | operator/= (rational const &rhs)
| Division assignment operator.
rational & | operator/= (value_type const &rhs)
| Division assignment operator.
rational & | operator++ ()
| Pre-increment.
rational & | operator-- ()
| Pre-decrement.
rational | operator++ (int)
| Post-increment.
rational | operator-- (int)
| Post-decrement.
## Private Member Functions
void | reduce ()
| Reduce the numerator and denominator by their GCD.
void | normalize ()
template<class U>
value_type | scale (U value)
## Private Attributes
value_type | numerator_
value_type | denominator_
## Classes
class | zero_denominator
| Exception class if the denominator is ever zero. More...

## Detailed Description

### template<class T>
class rational< T >

Represent a rational number (fraction) as a numerator and denominator.

Definition at line 16 of file rational.hpp.

## Member Typedef Documentation

template<class T>

typedef T rational< T >::value_type

Convenience typedef for the integral type of the numerator and denominator.

Definition at line 20 of file rational.hpp.

## Constructor & Destructor Documentation

template<class T>

rational< T >::rational | ( | value_type | num = `0` | ) | `[inline]`

Default constructor and constructor from a single value. As a default constructor, initializes to zero. Otherwise, initializes to the integer `num`.

Parameters:

  | num | The integer value to use as the initial value

Definition at line 33 of file rational.hpp.

    00033 : numerator_(num), denominator_(1) {}

template<class T>

rational< T >::rational | ( | value_type | num,
|  | value_type | den |
| ) |  |  | `[inline]`

Construct a rational number

Parameters:

  | num | numerator
  | den | denominator

Exceptions:

  | zero_denominator | if `den` == 0

Definition at line 106 of file rational.hpp.

References rational< T >::normalize().

    00107 : numerator_(num),
    00108   denominator_(den == value_type() ? throw zero_denominator("zero denominator") : den)
    00109 {
    00110   normalize();
    00111 }

template<class T>

rational< T >::rational | ( | double | r | ) | `[inline]`

Initialize the rational number with an approximation of `r`

Parameters:

  | r | the initial value

Definition at line 114 of file rational.hpp.

    00115 : numerator_(static_cast<T>(r / 100000)), denominator_(static_cast<T>(100000))
    00116 {}

template<class T>
template<class U>

rational< T >::rational | ( | rational< U > const & | that | ) | `[inline]`

Copy from a different type of rational.

Definition at line 120 of file rational.hpp.

References rational< T >::reduce().

    00121 : numerator_(scale<U>(that.numerator())), denominator_(scale<U>(that.denominator()))
    00122 {
    00123   reduce();
    00124 }

## Member Function Documentation

template<class T>

value_type rational< T >::numerator | ( |  | ) | const`[inline]`

Return the numerator.

Definition at line 47 of file rational.hpp.

Referenced by absval(), rational< long >::as(), number_rational::do_add(), number_rational::do_divide(), number_rational::do_multiply(), number_rational::do_subtract(), rational< T >::operator *=(), rational< T >::operator+=(), operator-(), rational< T >::operator-=(), operator/(), rational< T >::operator/=(), rational< T >::operator=(), operator==(), and rational< T >::reduce().

    00047 { return numerator_; }

template<class T>

value_type rational< T >::denominator | ( |  | ) | const`[inline]`

Return the denominator.

Definition at line 49 of file rational.hpp.

Referenced by absval(), rational< long >::as(), number_rational::do_add(), number_rational::do_divide(), number_rational::do_multiply(), number_rational::do_subtract(), rational< T >::operator *=(), rational< T >::operator++(), rational< T >::operator+=(), operator-(), rational< T >::operator--(), rational< T >::operator-=(), operator/(), rational< T >::operator/=(), rational< T >::operator=(), operator==(), and rational< T >::reduce().

    00049 { return denominator_; }

template<class T>
template<class U>

U rational< T >::as | ( |  | ) | const`[inline]`

Convert the rational number to another type, especially floating-point.

Definition at line 52 of file rational.hpp.

    00052 { return static_cast<U>(numerator()) / denominator(); }

template<class T>

rational< T > & rational< T >::operator= | ( | value_type | num | ) | `[inline]`

Assignment of an integer.

Definition at line 158 of file rational.hpp.

References rational< T >::denominator_, and rational< T >::numerator_.

    00159 {
    00160   numerator_ = num;
    00161   denominator_ = value_type(1);
    00162   return *this;
    00163 }

template<class T>
template<class U>

rational< T > & rational< T >::operator= | ( | rational< U > const & | rhs | ) | `[inline]`

Assignment of a rational with a different size.

Definition at line 167 of file rational.hpp.

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

    00168 {
    00169   numerator_ = scale<U>(rhs.numerator());
    00170   denominator_ = scale<U>(rhs.denominator());
    00171   reduce();
    00172   return *this;
    00173 }

template<class T>

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

Addition assignment operator.

Definition at line 176 of file rational.hpp.

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

    00177 {
    00178   numerator_ = numerator() * rhs.denominator() + rhs.numerator() * denominator();
    00179   denominator_ *= rhs.denominator();
    00180   reduce();
    00181   return *this;
    00182 }

template<class T>

rational< T > & rational< T >::operator+= | ( | value_type const & | rhs | ) | `[inline]`

Addition assignment operator.

Definition at line 185 of file rational.hpp.

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

    00186 {
    00187   numerator_ = numerator() + rhs * denominator();
    00188   reduce();
    00189   return *this;
    00190 }

template<class T>

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

Subtraction assignment operator.

Definition at line 193 of file rational.hpp.

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

    00194 {
    00195   numerator_ = numerator() * rhs.denominator() - rhs.numerator() * denominator();
    00196   denominator_ *= rhs.denominator();
    00197   reduce();
    00198   return *this;
    00199 }

template<class T>

rational< T > & rational< T >::operator-= | ( | value_type const & | rhs | ) | `[inline]`

Subtraction assignment operator.

Definition at line 202 of file rational.hpp.

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

    00203 {
    00204   numerator_ = numerator() - rhs * denominator();
    00205   reduce();
    00206   return *this;
    00207 }

template<class T>

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

Multiplication assignment operator.

Definition at line 210 of file rational.hpp.

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

    00211 {
    00212   numerator_ *= rhs.numerator();
    00213   denominator_ *= rhs.denominator();
    00214   reduce();
    00215   return *this;
    00216 }

template<class T>

rational< T > & rational< T >::operator *= | ( | value_type const & | rhs | ) | `[inline]`

Multiplication assignment operator.

Definition at line 219 of file rational.hpp.

References rational< T >::numerator_, and rational< T >::reduce().

    00220 {
    00221   numerator_ *= rhs;
    00222   reduce();
    00223   return *this;
    00224 }

template<class T>

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

Division assignment operator.

Definition at line 227 of file rational.hpp.

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

    00228 {
    00229   if (rhs.numerator() == value_type())
    00230     throw zero_denominator("divide by zero");
    00231   numerator_ *= rhs.denominator();
    00232   denominator_ *= rhs.numerator();
    00233   normalize();
    00234   return *this;
    00235 }

template<class T>

rational< T > & rational< T >::operator/= | ( | value_type const & | rhs | ) | `[inline]`

Division assignment operator.

Definition at line 238 of file rational.hpp.

References rational< T >::denominator_, and rational< T >::normalize().

    00239 {
    00240   if (rhs == value_type())
    00241     throw zero_denominator("divide by zero");
    00242   denominator_ *= rhs;
    00243   normalize();
    00244   return *this;
    00245 }

template<class T>

rational< T > & rational< T >::operator++ | ( |  | ) | `[inline]`

Pre-increment.

Definition at line 248 of file rational.hpp.

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

    00249 {
    00250   numerator_ += denominator();
    00251   return *this;
    00252 }

template<class T>

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

Pre-decrement.

Definition at line 263 of file rational.hpp.

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

    00264 {
    00265   numerator_ -= denominator();
    00266   return *this;
    00267 }

template<class T>

rational< T > rational< T >::operator++ | ( | int |  | ) | `[inline]`

Post-increment.

Definition at line 255 of file rational.hpp.

    00256 {
    00257   rational result(*this);
    00258   ++*this;
    00259   return result;
    00260 }

template<class T>

rational< T > rational< T >::operator-- | ( | int |  | ) | `[inline]`

Post-decrement.

Definition at line 270 of file rational.hpp.

    00271 {
    00272   rational result(*this);
    00273   --*this;
    00274   return result;
    00275 }

template<class T>

void rational< T >::reduce | ( |  | ) | `[inline, private]`

Reduce the numerator and denominator by their GCD.

Definition at line 148 of file rational.hpp.

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

Referenced by rational< T >::normalize(), rational< T >::operator *=(), rational< T >::operator+=(), rational< T >::operator-=(), rational< T >::operator=(), and rational< T >::rational().

    00149 {
    00150   value_type div(gcd(numerator(), denominator()));
    00151   if (div == value_type())
    00152     throw zero_denominator("zero denominator");
    00153   numerator_ /= div;
    00154   denominator_ /= div;
    00155 }

template<class T>

void rational< T >::normalize | ( |  | ) | `[inline, private]`

Reduce the numerator and denominator, and normalize the signs of both, that is, ensure denominator is not negative.

Definition at line 137 of file rational.hpp.

References rational< T >::denominator_, rational< T >::numerator_, and rational< T >::reduce().

Referenced by rational< T >::operator/=(), and rational< T >::rational().

    00138 {
    00139   if (denominator_ < value_type())
    00140   {
    00141     denominator_ = -denominator_;
    00142     numerator_ = -numerator_;
    00143   }
    00144   reduce();
    00145 }

template<class T>
template<class U>

T rational< T >::scale | ( | U | value | ) | `[inline, private]`

Scale an integer of type `U` to the value_type. If `U` has more digits than `value_type` shift `value` to the right.

Definition at line 128 of file rational.hpp.

    00129 {
    00130   if (std::numeric_limits<T>::digits >= std::numeric_limits<U>::digits)
    00131     return T(value);
    00132   else
    00133     return T(value >> (std::numeric_limits<U>::digits - std::numeric_limits<T>::digits));
    00134 }

## Member Data Documentation

template<class T>

value_type rational< T >::numerator_`[private]`

Definition at line 101 of file rational.hpp.

Referenced by rational< T >::normalize(), rational< long >::numerator(), rational< T >::operator *=(), rational< T >::operator++(), rational< T >::operator+=(), rational< T >::operator--(), rational< T >::operator-=(), rational< T >::operator/=(), rational< T >::operator=(), and rational< T >::reduce().

template<class T>

value_type rational< T >::denominator_`[private]`

Definition at line 102 of file rational.hpp.

Referenced by rational< long >::denominator(), rational< T >::normalize(), rational< T >::operator *=(), rational< T >::operator+=(), rational< T >::operator-=(), rational< T >::operator/=(), rational< T >::operator=(), and rational< T >::reduce().

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

- rational.hpp