Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Related Functions
vnl_rational Class Reference

High-precision rational numbers. More...

#include <vnl_rational.h>

List of all members.

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)
 Multiply/assign: replace lhs by lhs * rhs.
vnl_rationaloperator/= (vnl_rational const &r)
 Divide/assign: replace lhs by lhs / rhs.
vnl_rationaloperator/= (long r)
 Divide/assign: replace lhs by lhs / rhs.
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.)

void vsl_b_write (vsl_b_ostream &os, vnl_rational const &v)
 Binary save vnl_rational to stream.
void vsl_b_read (vsl_b_istream &is, vnl_rational &v)
 Binary load vnl_rational from stream.
void vsl_print_summary (vcl_ostream &os, vnl_rational const &b)
 Print human readable summary of object to a stream.
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.

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 69 of file vnl_rational.h.


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 80 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 87 of file vnl_rational.h.

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

Definition at line 89 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.

This is done by computing the continued fraction approximation for d.

Definition at line 11 of file vnl_rational.cxx.

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

Definition at line 96 of file vnl_rational.h.

vnl_rational::~vnl_rational ( ) [inline]

Definition at line 99 of file vnl_rational.h.


Member Function Documentation

vnl_rational vnl_rational::abs ( ) const [inline]

Returns the absolute value of the current rational.

Definition at line 128 of file vnl_rational.h.

long vnl_rational::ceil ( ) const [inline]

Converts rational value to integer by truncating towards positive infinity.

Definition at line 226 of file vnl_rational.h.

long vnl_rational::denominator ( ) const [inline]

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

Definition at line 106 of file vnl_rational.h.

long vnl_rational::floor ( ) const [inline]

Converts rational value to integer by truncating towards negative infinity.

Definition at line 223 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 254 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 131 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 263 of file vnl_rational.h.

long vnl_rational::numerator ( ) const [inline]

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

Definition at line 104 of file vnl_rational.h.

vnl_rational::operator double ( ) const [inline]

Definition at line 249 of file vnl_rational.h.

vnl_rational::operator double ( ) [inline]

Definition at line 250 of file vnl_rational.h.

vnl_rational::operator float ( ) const [inline]

Definition at line 247 of file vnl_rational.h.

vnl_rational::operator float ( ) [inline]

Definition at line 248 of file vnl_rational.h.

vnl_rational::operator int ( ) [inline]

Definition at line 240 of file vnl_rational.h.

vnl_rational::operator long ( ) const [inline]

Definition at line 245 of file vnl_rational.h.

vnl_rational::operator long ( ) [inline]

Definition at line 246 of file vnl_rational.h.

vnl_rational::operator short ( ) [inline]

Definition at line 235 of file vnl_rational.h.

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

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

Definition at line 126 of file vnl_rational.h.

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

Definition at line 115 of file vnl_rational.h.

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

Definition at line 117 of file vnl_rational.h.

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

Definition at line 119 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 177 of file vnl_rational.h.

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

Definition at line 186 of file vnl_rational.h.

vnl_rational & vnl_rational::operator*= ( vnl_rational const &  r)

Multiply/assign: replace lhs by lhs * rhs.

Note that 0 * Inf and Inf * 0 are undefined. Also note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.

Definition at line 36 of file vnl_rational.cxx.

vnl_rational & vnl_rational::operator*= ( long  r)

Multiply/assign: replace lhs by lhs * rhs.

Note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.

Definition at line 55 of file vnl_rational.cxx.

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

Unary plus - returns the current rational.

Definition at line 124 of file vnl_rational.h.

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

Pre-increment (++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 193 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 136 of file vnl_rational.h.

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

Definition at line 144 of file vnl_rational.h.

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

Unary minus - returns the negation of the current rational.

Definition at line 122 of file vnl_rational.h.

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

Pre-decrement (--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 195 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 147 of file vnl_rational.h.

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

Definition at line 155 of file vnl_rational.h.

vnl_rational & vnl_rational::operator/= ( vnl_rational const &  r)

Divide/assign: replace lhs by lhs / rhs.

Note that 0 / 0 and Inf / Inf are undefined. Also note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.

Definition at line 71 of file vnl_rational.cxx.

vnl_rational & vnl_rational::operator/= ( long  r)

Divide/assign: replace lhs by lhs / rhs.

Note that 0 / 0 is undefined. Also note that there could be integer overflow during this calculation! In that case, an approximate result will be returned.

Definition at line 91 of file vnl_rational.cxx.

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

Definition at line 197 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< ( int  r) const [inline]

Definition at line 211 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<= ( vnl_rational const &  r) const [inline]

Definition at line 205 of file vnl_rational.h.

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

Definition at line 209 of file vnl_rational.h.

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

Definition at line 213 of file vnl_rational.h.

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

Definition at line 217 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 109 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 113 of file vnl_rational.h.

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

Definition at line 116 of file vnl_rational.h.

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

Definition at line 118 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 208 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 216 of file vnl_rational.h.

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

Definition at line 206 of file vnl_rational.h.

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

Definition at line 210 of file vnl_rational.h.

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

Definition at line 214 of file vnl_rational.h.

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

Definition at line 218 of file vnl_rational.h.

long vnl_rational::round ( ) const [inline]

Rounds rational to nearest integer.

Definition at line 229 of file vnl_rational.h.

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

Definition at line 101 of file vnl_rational.h.

long vnl_rational::truncate ( ) const [inline]

Converts rational value to integer by truncating towards zero.

Definition at line 221 of file vnl_rational.h.


Friends And Related Function Documentation

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

Returns the remainder of r1 divided by r2.

Definition at line 400 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 346 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 292 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 319 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 373 of file vnl_rational.h.

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

formatted output.

Definition at line 277 of file vnl_rational.h.

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

simple input.

Definition at line 284 of file vnl_rational.h.

void vsl_b_read ( vsl_b_istream &  is,
vnl_rational v 
) [related]

Binary load vnl_rational from stream.

Definition at line 23 of file vnl_io_rational.cxx.

void vsl_b_write ( vsl_b_ostream &  os,
vnl_rational const &  v 
) [related]

Binary save vnl_rational to stream.

Definition at line 13 of file vnl_io_rational.cxx.

void vsl_print_summary ( vcl_ostream &  os,
vnl_rational const &  b 
) [related]

Print human readable summary of object to a stream.

Definition at line 47 of file vnl_io_rational.cxx.


Member Data Documentation

long vnl_rational::den_ [private]

Denominator portion.

Definition at line 72 of file vnl_rational.h.

long vnl_rational::num_ [private]

Numerator portion.

Definition at line 71 of file vnl_rational.h.


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