RationalNumber Class Reference
[AlgebraSemiring]

Rational numbers class. More...

List of all members.

Public Member Functions

std::ostream & print (std::ostream &ostr) const
Accessors
Get the numerator.

const int & num () const
const unsigned & denom () const
 Get the denominator.
Usual Operators.
Usual numerical operator.

RationalNumber operator+ (const RationalNumber &nb) const
RationalNumber operator- (const RationalNumber &nb) const
RationalNumber operator- () const
RationalNumber operator * (const RationalNumber &nb) const
RationalNumber operator/ (const RationalNumber &nb) const
RationalNumberoperator+= (const RationalNumber &nb)
RationalNumberoperator-= (const RationalNumber &nb)
RationalNumberoperator *= (const RationalNumber &nb)
RationalNumberoperator/= (const RationalNumber &nb)
bool operator< (const RationalNumber &nb) const
bool operator> (const RationalNumber &nb) const
bool operator<= (const RationalNumber &nb) const
bool operator>= (const RationalNumber &nb) const
bool operator== (const RationalNumber &nb) const
bool operator!= (const RationalNumber &nb) const
Type conversion methods.
int to_int () const
 Provide Explicit cast operator.
double to_double () const
 Provide Explicit cast operator.

Protected Member Functions

Fraction simplification
RationalNumberset_result ()
 Simplifies the fraction.
RationalNumberset_result (int num, unsigned int denom)
 Simplifies the fraction.

Protected Attributes

int num_
unsigned int denom_


Detailed Description

Rational numbers class.

This is a rational numbers implementation.

The way the constructor works ables us to work only on simplified fractions. So, the numerator (num_) and denominator (denom_) are always relatively prime.

Even after operations, the obtained fraction is simplified.

Author:
Sarah O'Connor <sarah@lrde.epita.fr>

Definition at line 59 of file rational_number.hh.


Constructor & Destructor Documentation

RationalNumber ( const T  num  )  [explicit]

Generic constructor.

Precondition:
  • T should be implicitly convertible into an integer representation.
  • T should be implicitly constructible from an integer.
  • T should conform the following prerequisite: int (T (n)) / int (T (1)) == n

Definition at line 60 of file rational_number.hxx.

RationalNumber ( const T  num  )  [explicit]

Generic constructor.

Precondition:
  • T should be implicitly convertible into an integer representation.
  • T should be implicitly constructible from an integer.
  • T should conform the following prerequisite: int (T (n)) / int (T (1)) == n

Definition at line 60 of file rational_number.hxx.


Member Function Documentation

int to_int (  )  const [inline]

Provide Explicit cast operator.

to_int() and to_double() allow us to get respectively an integer and a double from the rational number. These numbers are obtained by dividing the fraction's numerator and denominator.

Definition at line 229 of file rational_number.hxx.

References RationalNumber::denom_, RationalNumber::num_, and precondition.

double to_double (  )  const [inline]

Provide Explicit cast operator.

to_int() and to_double() allow us to get respectively an integer and a double from the rational number. These numbers are obtained by dividing the fraction's numerator and denominator.

Definition at line 236 of file rational_number.hxx.

References RationalNumber::denom_, and RationalNumber::num_.

RationalNumber & set_result (  )  [inline, protected]

Simplifies the fraction.

set_result() is used to simplify the fraction. We use the GCD (Greatest Common Divisor) algorithm. When there are no arguments, it simply checks the numerator and denominator and modifies them if needed.

Definition at line 87 of file rational_number.hxx.

References RationalNumber::denom_, vcsn::algebra::gcd(), and RationalNumber::num_.

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

RationalNumber & set_result ( int  num,
unsigned int  denom 
) [inline, protected]

Simplifies the fraction.

set_result() is used to simplify the fraction. We use the GCD (Greatest Common Divisor) algorithm. When there are no arguments, it simply checks the numerator and denominator and modifies them if needed.

Definition at line 78 of file rational_number.hxx.

References RationalNumber::denom_, vcsn::algebra::gcd(), and RationalNumber::num_.


Generated on Wed Jun 13 17:03:30 2007 for Vaucanson by  doxygen 1.5.1