rational Class Reference

The class rational represents rational number. More...

#include <rational.hh>

List of all members.

Public Member Functions

 rational (int n=0, int d=1)
 Constructor.
void set_denom (int d)
 Expand fraction to given denominator.
rationalcancel ()
 Cancel common factors from the fraction.
rationaloperator*= (const rational &other)
 Multiplication.
rational operator- (const rational &other) const
 Difference.
bool operator! () const
 Implicit conversion to bool and negation.
bool operator== (const rational &other) const
bool operator< (const rational &other) const
bool operator<= (const rational &other) const
bool operator!= (const rational &other) const
bool operator>= (const rational &other) const
bool operator> (const rational &other) const

Public Attributes

int nom
 Nomionator.
int denom
 Denominator.

Detailed Description

The class rational represents rational number.

A rational number is represented by giving a nominator and a denominator. Common factors need not necessarily be canceled from the fraction.

Definition at line 38 of file rational.hh.


Constructor & Destructor Documentation

rational::rational ( int  n = 0,
int  d = 1 
) [inline]

Constructor.

Depending on the number of arguments, this constructor can represent the default value of zero, an integer with denominator one, or a full fraction with nominator and denominator.

Parameters:
n the nominator of the rational number
d the denominator of the rational number

Definition at line 61 of file rational.hh.

Referenced by operator-().


Member Function Documentation

rational & rational::cancel (  ) 

Cancel common factors from the fraction.

This is done by dividing both nominator and denominator by their greatest common divisor.

Definition at line 51 of file rational.cc.

References denom, gcd(), and nom.

Referenced by operator*=().

bool rational::operator! (  )  const [inline]

Implicit conversion to bool and negation.

Returns:
true if the nominator is zero.

Definition at line 83 of file rational.hh.

References nom.

rational & rational::operator*= ( const rational other  ) 

Multiplication.

Parameters:
other the second factor.
Returns:
the product of this * other.

Definition at line 63 of file rational.cc.

References cancel(), denom, and nom.

rational rational::operator- ( const rational other  )  const

Difference.

Parameters:
other the subtrahend of the difference.
Returns:
the result of the difference this - other.

Definition at line 75 of file rational.cc.

References denom, lcm(), nom, and rational().

void rational::set_denom ( int  d  )  [inline]

Expand fraction to given denominator.

The new denominator must be a multiple of the current denominator. The intended use is to calculate the common denominator of a set of rationals and then expand each member of the set to this denominator.

Parameters:
d the new denominator, multiple of current denominator

Definition at line 73 of file rational.hh.

References denom, and nom.


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

Generated on Fri Aug 21 08:15:08 2009 for mqn2mps by  doxygen 1.6.0