Arageli::rational< T > Class Template Reference

Implementation of a Rational Number. More...

#include <rational.hpp>

Inheritance diagram for Arageli::rational< T >:

Arageli::rational_base List of all members.

Public Types

typedef T value_type
 Type of numerator and denominator.

Public Member Functions

 rational ()
 Constructs zero.
 rational (const char *str)
 Initialize rational from string notation.
template<typename T1>
 rational (const rational< T1 > &x)
 Creates a copy of the some rational.
template<typename T1>
 rational (const T1 &w)
 Creates a rational from integer value w.
 rational (const T &u, const T &v)
 Creates the rational w/v.
 rational (double x)
 operator int () const
 Convertion to int. Temporary implementation.
 operator big_int () const
 Convertion to big_int. Temporary implementation.
 operator float () const
 Converts from rational to float.
 operator double () const
 Converts from rational to double.
 operator long double () const
 Converts from rational to long double.
 operator bool () const
 Returns true if the rational is nonzero.
bool operator! () const
template<typename T1>
rationaloperator= (const rational< T1 > &x)
 Assigns numerator and denominator from x to this rational.
template<typename T1>
rationaloperator= (const T1 &x)
 Assigns this rationals with integer x.
rationaloperator= (double x)
rationaloperator= (const char *s)
const T & numerator () const
 Access to numerator. Read-only.
const T & denominator () const
 Access to denominator. Read-only.
T & numerator ()
 Access to numerator (non-constant).
T & denominator ()
 Access to denominator (non-constant).
rational operator- () const
 Returns a negated rational.
const rationaloperator+ () const
 Return a reference to this object. For sameness only.
rationaloperator++ ()
rational operator++ (int)
rationaloperator-- ()
rational operator-- (int)
rationalopposite ()
 Makes opposite rational.
rationalinverse ()
 Inverses this rational.
int sign () const
 Sign of a number.
bool is_integer () const
 Returns true if this rational is integer.
bool is_null () const
 Return true if this rational is zero.
bool is_unit () const
 Return true if this rational is unit.
bool is_opposite_unit () const
 Return true if this rational is opposite unit.
void normalize ()
 Reduces fraction numerator/denominator.
bool is_normal () const
 Returns true if the fraction numerator/denominator is irreducible.
template<typename T1>
void swap (rational< T1 > &x)
 Swaps two rationals without copying (if possible).

Detailed Description

template<typename T = big_int>
class Arageli::rational< T >

Implementation of a Rational Number.

An instance of this type is a rational number where the numerator and the denominator are both of type T.

Parameters:
T a type of numerator and denominator, by default it is big_int

Definition at line 68 of file rational.hpp.


Member Typedef Documentation

template<typename T = big_int>
typedef T Arageli::rational< T >::value_type

Type of numerator and denominator.

Definition at line 76 of file rational.hpp.


Constructor & Destructor Documentation

template<typename T = big_int>
Arageli::rational< T >::rational (  )  [inline]

Constructs zero.

Definition at line 79 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::rational ( const char *  str  ) 

Initialize rational from string notation.

template<typename T = big_int>
template<typename T1>
Arageli::rational< T >::rational ( const rational< T1 > &  x  )  [inline]

Creates a copy of the some rational.

Definition at line 86 of file rational.hpp.

template<typename T = big_int>
template<typename T1>
Arageli::rational< T >::rational ( const T1 &  w  )  [inline]

Creates a rational from integer value w.

After this, numerator = w, denominator = 1.

Definition at line 100 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::rational ( const T &  u,
const T &  v 
) [inline]

Creates the rational w/v.

After this, numerator = w, denominator = v.

Definition at line 104 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::rational ( double  x  )  [inline]

Definition at line 108 of file rational.hpp.


Member Function Documentation

template<typename T = big_int>
T& Arageli::rational< T >::denominator (  )  [inline]

Access to denominator (non-constant).

Definition at line 243 of file rational.hpp.

template<typename T = big_int>
const T& Arageli::rational< T >::denominator (  )  const [inline]

Access to denominator. Read-only.

Definition at line 237 of file rational.hpp.

template<typename T = big_int>
rational& Arageli::rational< T >::inverse (  ) 

Inverses this rational.

Performs swap between numerator and denominator. Requirement: denominator must not be equal to zero. Returns reference to the rational.

template<typename T = big_int>
bool Arageli::rational< T >::is_integer (  )  const [inline]

Returns true if this rational is integer.

Returns true if denominator = 1.

Definition at line 294 of file rational.hpp.

template<typename T = big_int>
bool Arageli::rational< T >::is_normal (  )  const

Returns true if the fraction numerator/denominator is irreducible.

After calls of all methods (except non-constant form of numerator() and denominator()) the result of is_normal() is true.

template<typename T = big_int>
bool Arageli::rational< T >::is_null (  )  const [inline]

Return true if this rational is zero.

Return true if numerator = 0.

Definition at line 298 of file rational.hpp.

template<typename T = big_int>
bool Arageli::rational< T >::is_opposite_unit (  )  const [inline]

Return true if this rational is opposite unit.

Return true if numerator = -1, denominator = 1.

Definition at line 306 of file rational.hpp.

template<typename T = big_int>
bool Arageli::rational< T >::is_unit (  )  const [inline]

Return true if this rational is unit.

Return true if numerator = 1, denominator = 1.

Definition at line 302 of file rational.hpp.

template<typename T = big_int>
void Arageli::rational< T >::normalize (  ) 

Reduces fraction numerator/denominator.

This method is useful if you use non-constant form of numerator() and denominator() functions and "manually" alternate their values. After call this function is_normal() always returns true.

template<typename T = big_int>
T& Arageli::rational< T >::numerator (  )  [inline]

Access to numerator (non-constant).

Definition at line 240 of file rational.hpp.

template<typename T = big_int>
const T& Arageli::rational< T >::numerator (  )  const [inline]

Access to numerator. Read-only.

Definition at line 234 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::operator big_int (  )  const [inline]

Convertion to big_int. Temporary implementation.

Definition at line 143 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::operator bool (  )  const [inline]

Returns true if the rational is nonzero.

Definition at line 187 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::operator double (  )  const [inline]

Converts from rational to double.

Just returns the result of division double(numerator)/double(denominator). WARNING! This is restricted implementation. For some values of numerator and denominator this solution doesn't work (converting to double may cause overflow even if the ration numerator/denominator in real sense is representable with available precision as double).

Definition at line 167 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::operator float (  )  const [inline]

Converts from rational to float.

Just returns the result of division float(numerator)/float(denominator). WARNING! This is restricted implementation. For some values of numerator and denominator this solution doesn't work (converting to double may cause overflow even if the ration numerator/denominator in real sense is representable with available precision as double).

Definition at line 155 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::operator int (  )  const [inline]

Convertion to int. Temporary implementation.

Definition at line 140 of file rational.hpp.

template<typename T = big_int>
Arageli::rational< T >::operator long double (  )  const [inline]

Converts from rational to long double.

Just returns the result of division (long double)(numerator)/(long double)(denominator). WARNING! This is restricted implementation. For some values of numerator and denominator this solution doesn't work (converting to double may cause overflow even if the ration numerator/denominator in real sense is representable with available precision as double).

Definition at line 180 of file rational.hpp.

template<typename T = big_int>
bool Arageli::rational< T >::operator! (  )  const [inline]

Definition at line 193 of file rational.hpp.

template<typename T = big_int>
const rational& Arageli::rational< T >::operator+ (  )  const [inline]

Return a reference to this object. For sameness only.

Definition at line 251 of file rational.hpp.

template<typename T = big_int>
rational Arageli::rational< T >::operator++ ( int   )  [inline]

Definition at line 261 of file rational.hpp.

template<typename T = big_int>
rational& Arageli::rational< T >::operator++ (  )  [inline]

Definition at line 254 of file rational.hpp.

template<typename T = big_int>
rational Arageli::rational< T >::operator- (  )  const [inline]

Returns a negated rational.

Returns a rational with numerator = - this numerator and the same denominator.

Definition at line 248 of file rational.hpp.

template<typename T = big_int>
rational Arageli::rational< T >::operator-- ( int   )  [inline]

Definition at line 270 of file rational.hpp.

template<typename T = big_int>
rational& Arageli::rational< T >::operator-- (  )  [inline]

Definition at line 263 of file rational.hpp.

template<typename T = big_int>
rational& Arageli::rational< T >::operator= ( const char *  s  )  [inline]

Definition at line 229 of file rational.hpp.

template<typename T = big_int>
rational& Arageli::rational< T >::operator= ( double  x  )  [inline]

Definition at line 225 of file rational.hpp.

template<typename T = big_int>
template<typename T1>
rational& Arageli::rational< T >::operator= ( const T1 &  x  )  [inline]

Assigns this rationals with integer x.

After this call numerator = x, denominator = 1.

Definition at line 218 of file rational.hpp.

template<typename T = big_int>
template<typename T1>
rational& Arageli::rational< T >::operator= ( const rational< T1 > &  x  )  [inline]

Assigns numerator and denominator from x to this rational.

Definition at line 203 of file rational.hpp.

template<typename T = big_int>
rational& Arageli::rational< T >::opposite (  )  [inline]

Makes opposite rational.

Calls 'value_traitsopposite' for numerator of this rational. Returns reference to the rational.

Definition at line 276 of file rational.hpp.

template<typename T = big_int>
int Arageli::rational< T >::sign (  )  const

Sign of a number.

Returns

template<typename T = big_int>
template<typename T1>
void Arageli::rational< T >::swap ( rational< T1 > &  x  )  [inline]

Swaps two rationals without copying (if possible).

Definition at line 323 of file rational.hpp.


The documentation for this class was generated from the following file:
Generated on Thu Aug 31 17:38:27 2006 for Arageli by  doxygen 1.4.7