vnl_rational Class Reference

#include <vnl_rational.h>

List of all members.


Detailed Description

High-precision rational numbers.

The vnl_rational class provides high-precision rational numbers and arithmetic, using the built-in type long, for the numerator and denominator. Implicit conversion to the system defined types short, int, long, float, and double is supported by overloaded operator member functions. Although the rational class makes judicious use of inline functions and deals only with integral values, the user is warned that the rational integer arithmetic class is still considerably slower than the built-in integer data types. If the range of values anticipated will fit into a built-in type, use that instead.

In addition to the original COOL Rational class, vnl_rational is able to represent plus and minus infinity. An other interesting addition is the possibility to construct a rational from a double. This allows for lossless conversion from e.g. double 1.0/3.0 to the rational number 1/3, hence no more rounding errors. This is implemented with continued fraction approximations.

Definition at line 67 of file vnl_rational.h.


Public Member Functions

 vnl_rational (long num=0L, long den=1L)
 Creates a rational with given numerator and denominator.
 vnl_rational (int num, int den=1)
 Creates a rational with given numerator and denominator.
 vnl_rational (unsigned int num, unsigned int den=1)
 vnl_rational (double d)
 Creates a rational from a double.
 vnl_rational (vnl_rational const &from)
 ~vnl_rational ()
void set (long num, long den)
long numerator () const
 Return the numerator of the (simplified) rational number representation.
long denominator () const
 Return the denominator of the (simplified) rational number representation.
vnl_rationaloperator= (vnl_rational const &rhs)
 Copies the contents and state of rhs rational over to the lhs.
bool operator== (vnl_rational const &rhs) const
 Returns true if the two rationals have the same representation.
bool operator!= (vnl_rational const &rhs) const
bool operator== (long rhs) const
bool operator!= (long rhs) const
bool operator== (int rhs) const
bool operator!= (int rhs) const
vnl_rational operator- () const
 Unary minus - returns the negation of the current rational.
vnl_rational operator+ () const
 Unary plus - returns the current rational.
bool operator! () const
 Unary not - returns true if rational is equal to zero.
vnl_rational abs () const
 Returns the absolute value of the current rational.
vnl_rationalinvert ()
 Replaces rational with 1/rational and returns it.
vnl_rationaloperator+= (vnl_rational const &r)
 Plus/assign: replace lhs by lhs + rhs.
vnl_rationaloperator+= (long r)
vnl_rationaloperator-= (vnl_rational const &r)
 Minus/assign: replace lhs by lhs - rhs.
vnl_rationaloperator-= (long r)
vnl_rationaloperator *= (vnl_rational const &r)
 Multiply/assign: replace lhs by lhs * rhs.
vnl_rationaloperator *= (long r)
vnl_rationaloperator/= (vnl_rational const &r)
 Divide/assign: replace lhs by lhs / rhs.
vnl_rationaloperator/= (long r)
vnl_rationaloperator%= (vnl_rational const &r)
 Modulus/assign: replace lhs by lhs % rhs.
vnl_rationaloperator%= (long r)
vnl_rationaloperator++ ()
 Pre-increment (++r). No-op when +-Inf.
vnl_rationaloperator-- ()
 Pre-decrement (--r). No-op when +-Inf.
vnl_rational operator++ (int)
 Post-increment (r++). No-op when +-Inf.
vnl_rational operator-- (int)
 Post-decrement (r--). No-op when +-Inf.
bool operator< (vnl_rational const &rhs) const
bool operator> (vnl_rational const &r) const
bool operator<= (vnl_rational const &r) const
bool operator>= (vnl_rational const &r) const
bool operator< (long r) const
bool operator> (long r) const
bool operator<= (long r) const
bool operator>= (long r) const
bool operator< (int r) const
bool operator> (int r) const
bool operator<= (int r) const
bool operator>= (int r) const
bool operator< (double r) const
bool operator> (double r) const
bool operator<= (double r) const
bool operator>= (double r) const
long truncate () const
 Converts rational value to integer by truncating towards zero.
long floor () const
 Converts rational value to integer by truncating towards negative infinity.
long ceil () const
 Converts rational value to integer by truncating towards positive infinity.
long round () const
 Rounds rational to nearest integer.
 operator short ()
 operator int ()
 operator long () const
 operator long ()
 operator float () const
 operator float ()
 operator double () const
 operator double ()

Static Public Member Functions

static long gcd (long l1, long l2)
 Calculate greatest common divisor of two integers.

Private Member Functions

void normalize ()
 Private function to normalize numerator/denominator of rational number.

Private Attributes

long num_
 Numerator portion.
long den_
 Denominator portion.

Related Functions

(Note that these are not member functions.)

vcl_ostream & operator<< (vcl_ostream &s, vnl_rational const &r)
 formatted output.
vcl_istream & operator>> (vcl_istream &s, vnl_rational &r)
 simple input.
vnl_rational operator+ (vnl_rational const &r1, vnl_rational const &r2)
 Returns the sum of two rational numbers.
vnl_rational operator- (vnl_rational const &r1, vnl_rational const &r2)
 Returns the difference of two rational numbers.
vnl_rational operator * (vnl_rational const &r1, vnl_rational const &r2)
 Returns the product of two rational numbers.
vnl_rational operator/ (vnl_rational const &r1, vnl_rational const &r2)
 Returns the quotient of two rational numbers.
vnl_rational operator% (vnl_rational const &r1, vnl_rational const &r2)
 Returns the remainder of r1 divided by r2.

Constructor & Destructor Documentation

vnl_rational::vnl_rational ( long  num = 0L,
long  den = 1L 
) [inline]

Creates a rational with given numerator and denominator.

Default constructor gives 0. Also serves as automatic cast from long to vnl_rational. The only input which is not allowed is (0,0); the denominator is allowed to be 0, to represent +Inf or -Inf.

Definition at line 78 of file vnl_rational.h.

vnl_rational::vnl_rational ( int  num,
int  den = 1 
) [inline, explicit]

Creates a rational with given numerator and denominator.

Note these are not automatic type conversions because of a bug in the Borland compiler. Since these just convert their arguments to long anyway, there is no harm in letting the long overload be used for automatic conversions.

Definition at line 85 of file vnl_rational.h.

vnl_rational::vnl_rational ( unsigned int  num,
unsigned int  den = 1 
) [inline, explicit]

Definition at line 87 of file vnl_rational.h.

vnl_rational::vnl_rational ( double  d  )  [explicit]

Creates a rational from a double.

This is done by computing the continued fraction approximation for d. Note that this is explicitly *not* an automatic type conversion.

Definition at line 8 of file vnl_rational.cxx.

vnl_rational::vnl_rational ( vnl_rational const &  from  )  [inline]

Definition at line 94 of file vnl_rational.h.

vnl_rational::~vnl_rational (  )  [inline]

Definition at line 97 of file vnl_rational.h.


Member Function Documentation

void vnl_rational::set ( long  num,
long  den 
) [inline]

Definition at line 99 of file vnl_rational.h.

long vnl_rational::numerator (  )  const [inline]

Return the numerator of the (simplified) rational number representation.

Definition at line 102 of file vnl_rational.h.

long vnl_rational::denominator (  )  const [inline]

Return the denominator of the (simplified) rational number representation.

Definition at line 104 of file vnl_rational.h.

vnl_rational& vnl_rational::operator= ( vnl_rational const &  rhs  )  [inline]

Copies the contents and state of rhs rational over to the lhs.

Definition at line 107 of file vnl_rational.h.

bool vnl_rational::operator== ( vnl_rational const &  rhs  )  const [inline]

Returns true if the two rationals have the same representation.

Definition at line 111 of file vnl_rational.h.

bool vnl_rational::operator!= ( vnl_rational const &  rhs  )  const [inline]

Definition at line 113 of file vnl_rational.h.

bool vnl_rational::operator== ( long  rhs  )  const [inline]

Definition at line 114 of file vnl_rational.h.

bool vnl_rational::operator!= ( long  rhs  )  const [inline]

Definition at line 115 of file vnl_rational.h.

bool vnl_rational::operator== ( int  rhs  )  const [inline]

Definition at line 116 of file vnl_rational.h.

bool vnl_rational::operator!= ( int  rhs  )  const [inline]

Definition at line 117 of file vnl_rational.h.

vnl_rational vnl_rational::operator- (  )  const [inline]

Unary minus - returns the negation of the current rational.

Definition at line 120 of file vnl_rational.h.

vnl_rational vnl_rational::operator+ (  )  const [inline]

Unary plus - returns the current rational.

Definition at line 122 of file vnl_rational.h.

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

Unary not - returns true if rational is equal to zero.

Definition at line 124 of file vnl_rational.h.

vnl_rational vnl_rational::abs (  )  const [inline]

Returns the absolute value of the current rational.

Definition at line 126 of file vnl_rational.h.

vnl_rational& vnl_rational::invert (  )  [inline]

Replaces rational with 1/rational and returns it.

Inverting 0 gives +Inf, inverting +-Inf gives 0.

Definition at line 129 of file vnl_rational.h.

vnl_rational& vnl_rational::operator+= ( vnl_rational const &  r  )  [inline]

Plus/assign: replace lhs by lhs + rhs.

Note that +Inf + -Inf and -Inf + +Inf are undefined.

Definition at line 134 of file vnl_rational.h.

vnl_rational& vnl_rational::operator+= ( long  r  )  [inline]

Definition at line 142 of file vnl_rational.h.

vnl_rational& vnl_rational::operator-= ( vnl_rational const &  r  )  [inline]

Minus/assign: replace lhs by lhs - rhs.

Note that +Inf - +Inf and -Inf - -Inf are undefined.

Definition at line 145 of file vnl_rational.h.

vnl_rational& vnl_rational::operator-= ( long  r  )  [inline]

Definition at line 153 of file vnl_rational.h.

vnl_rational& vnl_rational::operator *= ( vnl_rational const &  r  )  [inline]

Multiply/assign: replace lhs by lhs * rhs.

Note that 0 * Inf and Inf * 0 are undefined.

Definition at line 156 of file vnl_rational.h.

vnl_rational& vnl_rational::operator *= ( long  r  )  [inline]

Definition at line 161 of file vnl_rational.h.

vnl_rational& vnl_rational::operator/= ( vnl_rational const &  r  )  [inline]

Divide/assign: replace lhs by lhs / rhs.

Note that 0 / 0 and Inf / Inf are undefined.

Definition at line 164 of file vnl_rational.h.

vnl_rational& vnl_rational::operator/= ( long  r  )  [inline]

Definition at line 169 of file vnl_rational.h.

vnl_rational& vnl_rational::operator%= ( vnl_rational const &  r  )  [inline]

Modulus/assign: replace lhs by lhs % rhs.

Note that r % Inf is r, and that r % 0 and Inf % r are undefined.

Definition at line 175 of file vnl_rational.h.

vnl_rational& vnl_rational::operator%= ( long  r  )  [inline]

Definition at line 184 of file vnl_rational.h.

vnl_rational& vnl_rational::operator++ (  )  [inline]

Pre-increment (++r). No-op when +-Inf.

Definition at line 187 of file vnl_rational.h.

vnl_rational& vnl_rational::operator-- (  )  [inline]

Pre-decrement (--r). No-op when +-Inf.

Definition at line 189 of file vnl_rational.h.

vnl_rational vnl_rational::operator++ ( int   )  [inline]

Post-increment (r++). No-op when +-Inf.

Definition at line 191 of file vnl_rational.h.

vnl_rational vnl_rational::operator-- ( int   )  [inline]

Post-decrement (r--). No-op when +-Inf.

Definition at line 193 of file vnl_rational.h.

bool vnl_rational::operator< ( vnl_rational const &  rhs  )  const [inline]

Definition at line 195 of file vnl_rational.h.

bool vnl_rational::operator> ( vnl_rational const &  r  )  const [inline]

Definition at line 202 of file vnl_rational.h.

bool vnl_rational::operator<= ( vnl_rational const &  r  )  const [inline]

Definition at line 203 of file vnl_rational.h.

bool vnl_rational::operator>= ( vnl_rational const &  r  )  const [inline]

Definition at line 204 of file vnl_rational.h.

bool vnl_rational::operator< ( long  r  )  const [inline]

Definition at line 205 of file vnl_rational.h.

bool vnl_rational::operator> ( long  r  )  const [inline]

Definition at line 206 of file vnl_rational.h.

bool vnl_rational::operator<= ( long  r  )  const [inline]

Definition at line 207 of file vnl_rational.h.

bool vnl_rational::operator>= ( long  r  )  const [inline]

Definition at line 208 of file vnl_rational.h.

bool vnl_rational::operator< ( int  r  )  const [inline]

Definition at line 209 of file vnl_rational.h.

bool vnl_rational::operator> ( int  r  )  const [inline]

Definition at line 210 of file vnl_rational.h.

bool vnl_rational::operator<= ( int  r  )  const [inline]

Definition at line 211 of file vnl_rational.h.

bool vnl_rational::operator>= ( int  r  )  const [inline]

Definition at line 212 of file vnl_rational.h.

bool vnl_rational::operator< ( double  r  )  const [inline]

Definition at line 213 of file vnl_rational.h.

bool vnl_rational::operator> ( double  r  )  const [inline]

Definition at line 214 of file vnl_rational.h.

bool vnl_rational::operator<= ( double  r  )  const [inline]

Definition at line 215 of file vnl_rational.h.

bool vnl_rational::operator>= ( double  r  )  const [inline]

Definition at line 216 of file vnl_rational.h.

long vnl_rational::truncate (  )  const [inline]

Converts rational value to integer by truncating towards zero.

Definition at line 219 of file vnl_rational.h.

long vnl_rational::floor (  )  const [inline]

Converts rational value to integer by truncating towards negative infinity.

Definition at line 221 of file vnl_rational.h.

long vnl_rational::ceil (  )  const [inline]

Converts rational value to integer by truncating towards positive infinity.

Definition at line 224 of file vnl_rational.h.

long vnl_rational::round (  )  const [inline]

Rounds rational to nearest integer.

Definition at line 227 of file vnl_rational.h.

vnl_rational::operator short (  )  [inline]

Definition at line 233 of file vnl_rational.h.

vnl_rational::operator int (  )  [inline]

Definition at line 238 of file vnl_rational.h.

vnl_rational::operator long (  )  const [inline]

Definition at line 243 of file vnl_rational.h.

vnl_rational::operator long (  )  [inline]

Definition at line 244 of file vnl_rational.h.

vnl_rational::operator float (  )  const [inline]

Definition at line 245 of file vnl_rational.h.

vnl_rational::operator float (  )  [inline]

Definition at line 246 of file vnl_rational.h.

vnl_rational::operator double (  )  const [inline]

Definition at line 247 of file vnl_rational.h.

vnl_rational::operator double (  )  [inline]

Definition at line 248 of file vnl_rational.h.

static long vnl_rational::gcd ( long  l1,
long  l2 
) [inline, static]

Calculate greatest common divisor of two integers.

Used to simplify rational number.

Definition at line 252 of file vnl_rational.h.

void vnl_rational::normalize (  )  [inline, private]

Private function to normalize numerator/denominator of rational number.

If num_ and den_ are both nonzero, their gcd is made 1 and den_ made positive. Otherwise, the nonzero den_ is set to 1 or the nonzero num_ to +1 or -1.

Definition at line 261 of file vnl_rational.h.


Friends And Related Function Documentation

vcl_ostream & operator<< ( vcl_ostream &  s,
vnl_rational const &  r 
) [related]

formatted output.

Definition at line 275 of file vnl_rational.h.

vcl_istream & operator>> ( vcl_istream &  s,
vnl_rational r 
) [related]

simple input.

Definition at line 282 of file vnl_rational.h.

vnl_rational operator+ ( vnl_rational const &  r1,
vnl_rational const &  r2 
) [related]

Returns the sum of two rational numbers.

Definition at line 290 of file vnl_rational.h.

vnl_rational operator- ( vnl_rational const &  r1,
vnl_rational const &  r2 
) [related]

Returns the difference of two rational numbers.

Definition at line 317 of file vnl_rational.h.

vnl_rational operator * ( vnl_rational const &  r1,
vnl_rational const &  r2 
) [related]

Returns the product of two rational numbers.

Definition at line 344 of file vnl_rational.h.

vnl_rational operator/ ( vnl_rational const &  r1,
vnl_rational const &  r2 
) [related]

Returns the quotient of two rational numbers.

Definition at line 371 of file vnl_rational.h.

vnl_rational operator% ( vnl_rational const &  r1,
vnl_rational const &  r2 
) [related]

Returns the remainder of r1 divided by r2.

Definition at line 398 of file vnl_rational.h.


Member Data Documentation

long vnl_rational::num_ [private]

Numerator portion.

Definition at line 69 of file vnl_rational.h.

long vnl_rational::den_ [private]

Denominator portion.

Definition at line 70 of file vnl_rational.h.


The documentation for this class was generated from the following files:
Generated on Sun Sep 7 05:06:25 2008 for core/vnl by  doxygen 1.5.1