Rational< IntType > Class Template Reference

#include <Rational.h>

List of all members.

Public Member Functions

 Rational ()
 Rational (param_type n)
 Rational (param_type n, param_type d)
 Rational (const Rational &other)
Rationaloperator= (const Rational &other)
Rationaloperator= (param_type n)
Rationalassign (param_type n, param_type d)
const IntType & numerator () const
const IntType & denominator () const
Rationaloperator+= (const Rational &r)
Rationaloperator-= (const Rational &r)
Rationaloperator *= (const Rational &r)
Rationaloperator/= (const Rational &r)
Rational operator+ (const Rational &r) const
Rational operator- (const Rational &r) const
Rational operator * (const Rational &r) const
Rational operator/ (const Rational &r) const
Rationaloperator+= (param_type i)
Rationaloperator-= (param_type i)
Rationaloperator *= (param_type i)
Rationaloperator/= (param_type i)
const Rationaloperator++ ()
const Rationaloperator-- ()
bool operator! () const
bool operator< (const Rational &r) const
bool operator== (const Rational &r) const
bool operator> (const Rational &r) const
bool operator!= (const Rational &r) const
bool operator< (param_type i) const
bool operator> (param_type i) const
bool operator== (param_type i) const
void load (std::istream &is)
void save (std::ostream &os) const
std::string toString (bool decimalFormat=false, int precision=3) const
void display (std::ostream &os=std::cout) const


Detailed Description

template<typename IntType>
class Rational< IntType >

The Rational class is used in this math template library to provide exact precision arithmetic. The Rational class was taken from the "Boost" library (a very popular C++ library, http://www.boost.org/libs/rational) and was slightly adapted for the needs of the math template library. The Rational class represents rational numbers and is an alternative to the built in floating point types. A rational number is represented as a fraction of two integers and is always kept in normalized form.

Definition at line 121 of file Rational.h.


Constructor & Destructor Documentation

template<typename IntType>
Rational< IntType >::Rational (  )  [inline]

Default constructor.

Remarks:
Initializes the rational number to zero.

Definition at line 133 of file Rational.h.

template<typename IntType>
Rational< IntType >::Rational ( param_type  n  )  [inline]

Constructor from an int_type

Remarks:
The denominator is set to one.

Definition at line 140 of file Rational.h.

template<typename IntType>
Rational< IntType >::Rational ( param_type  n,
param_type  d 
) [inline]

Constructor

Parameters:
n The value of the numerator
d The value of the denominator
Remarks:
The fraction is normalized upon construction i.e. the greatest common divisor of the numerator and denominator is always one.

Definition at line 150 of file Rational.h.

template<typename IntType>
Rational< IntType >::Rational ( const Rational< IntType > &  other  )  [inline]

Copy constructor

Parameters:
other The other rational number to copy.

Definition at line 158 of file Rational.h.


Member Function Documentation

template<typename IntType>
Rational& Rational< IntType >::operator= ( const Rational< IntType > &  other  )  [inline]

Assignment operator

Parameters:
other The other rational number to copy.
Returns:
Refference to this rational number.

Definition at line 166 of file Rational.h.

References Rational< IntType >::den_, and Rational< IntType >::num_.

template<typename IntType>
Rational& Rational< IntType >::operator= ( param_type  n  )  [inline]

Assignment operator

Parameters:
n an integral value
Returns:
Refference to this rational number.

Definition at line 181 of file Rational.h.

References Rational< IntType >::assign().

Here is the call graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::assign ( param_type  n,
param_type  d 
) [inline]

In place assignment

Parameters:
n The new value of the numerator.
d The new value of the denominator.
Returns:
A refference to this rational number

Definition at line 491 of file Rational.h.

Referenced by Rational< IntType >::operator=().

Here is the caller graph for this function:

template<typename IntType>
const IntType& Rational< IntType >::numerator (  )  const [inline]

Return the numerator

Returns:
A const refference to the numerator value.

Definition at line 193 of file Rational.h.

Referenced by AlgebraicTraits< Rational< IntType > >::abs(), AlgebraicTraits< Rational< IntType > >::equals(), AlgebraicTraits< Rational< IntType > >::is_zero(), AlgebraicTraits< Rational< IntType > >::norm(), AlgebraicTraits< Rational< IntType > >::one(), and AlgebraicTraits< Rational< IntType > >::zero().

Here is the caller graph for this function:

template<typename IntType>
const IntType& Rational< IntType >::denominator (  )  const [inline]

Return the denominator

Returns:
A const refference to the denominator value.

Definition at line 201 of file Rational.h.

Referenced by AlgebraicTraits< Rational< IntType > >::abs(), AlgebraicTraits< Rational< IntType > >::equals(), and AlgebraicTraits< Rational< IntType > >::norm().

Here is the caller graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator+= ( const Rational< IntType > &  r  ) 

Addition assignment operator

Parameters:
r Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 522 of file Rational.h.

References Rational< IntType >::den_, and Rational< IntType >::num_.

Referenced by Rational< IntType >::operator+=().

Here is the caller graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator-= ( const Rational< IntType > &  r  ) 

Subtraction assignment operator

Parameters:
r Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 561 of file Rational.h.

References Rational< IntType >::den_, and Rational< IntType >::num_.

Referenced by Rational< IntType >::operator-=().

Here is the caller graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator *= ( const Rational< IntType > &  r  ) 

Multiplication assignment operator

Parameters:
r Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 584 of file Rational.h.

References Rational< IntType >::den_, and Rational< IntType >::num_.

Referenced by Rational< IntType >::operator *=().

Here is the caller graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator/= ( const Rational< IntType > &  r  ) 

Division assignment operator

Parameters:
r Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 603 of file Rational.h.

References Rational< IntType >::den_, and Rational< IntType >::num_.

Referenced by Rational< IntType >::operator/=().

Here is the caller graph for this function:

template<typename IntType>
Rational Rational< IntType >::operator+ ( const Rational< IntType > &  r  )  const [inline]

Addition operator

Parameters:
r Right hand side operator
Returns:
The result of the operation.

Definition at line 216 of file Rational.h.

template<typename IntType>
Rational Rational< IntType >::operator- ( const Rational< IntType > &  r  )  const [inline]

Subtraction operator

Parameters:
r Right hand side operator
Returns:
The result of the operation.

Definition at line 226 of file Rational.h.

template<typename IntType>
Rational Rational< IntType >::operator * ( const Rational< IntType > &  r  )  const [inline]

Multiplication operator

Parameters:
r Right hand side operator
Returns:
The result of the operation.

Definition at line 236 of file Rational.h.

template<typename IntType>
Rational Rational< IntType >::operator/ ( const Rational< IntType > &  r  )  const [inline]

Division operator

Parameters:
r Right hand side operator
Returns:
The result of the operation.

Definition at line 246 of file Rational.h.

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator+= ( param_type  i  )  [inline]

Addition assignment operator

Parameters:
i Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 642 of file Rational.h.

References Rational< IntType >::operator+=().

Here is the call graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator-= ( param_type  i  )  [inline]

Subtraction assignment operator

Parameters:
i Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 653 of file Rational.h.

References Rational< IntType >::operator-=().

Here is the call graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator *= ( param_type  i  )  [inline]

Multiplication assignment operator

Parameters:
i Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 664 of file Rational.h.

References Rational< IntType >::operator *=().

Here is the call graph for this function:

template<typename IntType>
Rational< IntType > & Rational< IntType >::operator/= ( param_type  i  )  [inline]

Division assignment operator

Parameters:
i Right hand side operator.
Returns:
A refference to this rational number.

Definition at line 675 of file Rational.h.

References Rational< IntType >::operator/=().

Here is the call graph for this function:

template<typename IntType>
const Rational< IntType > & Rational< IntType >::operator++ (  )  [inline]

Increment operator

Definition at line 684 of file Rational.h.

template<typename IntType>
const Rational< IntType > & Rational< IntType >::operator-- (  )  [inline]

Decrement operator

Definition at line 694 of file Rational.h.

template<typename IntType>
bool Rational< IntType >::operator! (  )  const [inline]

Test if value is different from zero

Returns:
true if rational number is zero and false otherwise

Definition at line 266 of file Rational.h.

template<typename IntType>
bool Rational< IntType >::operator< ( const Rational< IntType > &  r  )  const

Comparison operator

Parameters:
r Right hand side operator
Returns:
true is this is less than r and false otherwise
Remarks:
This operation can be quite expensive (2 greatest common divisor computations, 4 divisions, 2 multiplications)

Definition at line 709 of file Rational.h.

References Rational< IntType >::den_, and Rational< IntType >::num_.

Referenced by Rational< IntType >::operator>().

Here is the caller graph for this function:

template<typename IntType>
bool Rational< IntType >::operator== ( const Rational< IntType > &  r  )  const [inline]

Equality operator

Parameters:
r Right hand side operator
Returns:
true is this is equal to r and false otherwise

Definition at line 776 of file Rational.h.

References Rational< IntType >::den_, and Rational< IntType >::num_.

template<typename IntType>
bool Rational< IntType >::operator!= ( const Rational< IntType > &  r  )  const [inline]

Inequality operator

Parameters:
r Rational number to compare this to.
Returns:
true if this is different from r and false otherwise

Definition at line 284 of file Rational.h.

template<typename IntType>
bool Rational< IntType >::operator< ( param_type  i  )  const

Comparison operator

Parameters:
i Right hand side operator
Returns:
true is this is less than i and false otherwise

Definition at line 733 of file Rational.h.

template<typename IntType>
bool Rational< IntType >::operator> ( param_type  i  )  const

Comparison, greater than operator

Parameters:
i Right hand side operator
Returns:
true is this is greater than i and false otherwise

Definition at line 761 of file Rational.h.

References Rational< IntType >::operator<().

Here is the call graph for this function:

template<typename IntType>
bool Rational< IntType >::operator== ( param_type  i  )  const [inline]

Equality operator

Parameters:
i Right hand side operator
Returns:
true is this is equal to i and false otherwise

Definition at line 786 of file Rational.h.

template<typename IntType>
void Rational< IntType >::load ( std::istream &  is  )  [inline]

Load this from a stream

Parameters:
is stream to load from

Definition at line 387 of file Rational.h.

template<typename IntType>
void Rational< IntType >::save ( std::ostream &  os  )  const [inline]

Save this to a stream

Parameters:
os stream to save to

Definition at line 396 of file Rational.h.

template<typename IntType>
std::string Rational< IntType >::toString ( bool  decimalFormat = false,
int  precision = 3 
) const [inline]

Convert this rational number to a string

Parameters:
decimalFormat This parameter must be true if one desires to display the rational number in decimal format
precision The maximum number of fractional digits. This parameter is used only if decimalFormat is true.
Returns:
A string representation for this rational number.
Remarks:
If decimalFormat is true, the last digit in the result is not rounded

Definition at line 412 of file Rational.h.

Referenced by Rational< IntType >::display().

Here is the caller graph for this function:

template<typename IntType>
void Rational< IntType >::display ( std::ostream &  os = std::cout  )  const [inline]

Display this number to a stream.

Parameters:
os Stream to display to.

Definition at line 458 of file Rational.h.

References AlgebraicTraits< T >::one(), and Rational< IntType >::toString().

Here is the call graph for this function:


The documentation for this class was generated from the following file:
Math-Objects Library Docs  Generated by: doxygen