Classes | Namespaces | Functions
vnl_rational.h File Reference

High-precision rational numbers. More...

#include <iostream>
#include <cassert>
#include "vnl/vnl_export.h"

Go to the source code of this file.

Classes

class  vnl_rational
 High-precision rational numbers. More...
 

Namespaces

 vnl_math
 real numerical constants.
 

Functions

std::ostream & operator<< (std::ostream &s, vnl_rational const &r)
 formatted output. More...
 
std::istream & operator>> (std::istream &s, vnl_rational &r)
 simple input. More...
 
vnl_rational operator+ (vnl_rational const &r1, vnl_rational const &r2)
 Returns the sum of two rational numbers. More...
 
vnl_rational operator+ (vnl_rational const &r1, long r2)
 
vnl_rational operator+ (vnl_rational const &r1, int r2)
 
vnl_rational operator+ (long r2, vnl_rational const &r1)
 
vnl_rational operator+ (int r2, vnl_rational const &r1)
 
vnl_rational operator- (vnl_rational const &r1, vnl_rational const &r2)
 Returns the difference of two rational numbers. More...
 
vnl_rational operator- (vnl_rational const &r1, long r2)
 
vnl_rational operator- (vnl_rational const &r1, int r2)
 
vnl_rational operator- (long r2, vnl_rational const &r1)
 
vnl_rational operator- (int r2, vnl_rational const &r1)
 
vnl_rational operator * (vnl_rational const &r1, vnl_rational const &r2)
 Returns the product of two rational numbers. More...
 
vnl_rational operator * (vnl_rational const &r1, long r2)
 
vnl_rational operator * (vnl_rational const &r1, int r2)
 
vnl_rational operator * (long r2, vnl_rational const &r1)
 
vnl_rational operator * (int r2, vnl_rational const &r1)
 
vnl_rational operator/ (vnl_rational const &r1, vnl_rational const &r2)
 Returns the quotient of two rational numbers. More...
 
vnl_rational operator/ (vnl_rational const &r1, long r2)
 
vnl_rational operator/ (vnl_rational const &r1, int r2)
 
vnl_rational operator/ (long r1, vnl_rational const &r2)
 
vnl_rational operator/ (int r1, vnl_rational const &r2)
 
vnl_rational operator% (vnl_rational const &r1, vnl_rational const &r2)
 Returns the remainder of r1 divided by r2. More...
 
vnl_rational operator% (vnl_rational const &r1, long r2)
 
vnl_rational operator% (vnl_rational const &r1, int r2)
 
vnl_rational operator% (long r1, vnl_rational const &r2)
 
vnl_rational operator% (int r1, vnl_rational const &r2)
 
bool operator== (int r1, vnl_rational const &r2)
 
bool operator== (long r1, vnl_rational const &r2)
 
bool operator!= (int r1, vnl_rational const &r2)
 
bool operator!= (long r1, vnl_rational const &r2)
 
bool operator< (int r1, vnl_rational const &r2)
 
bool operator< (long r1, vnl_rational const &r2)
 
bool operator> (int r1, vnl_rational const &r2)
 
bool operator> (long r1, vnl_rational const &r2)
 
bool operator<= (int r1, vnl_rational const &r2)
 
bool operator<= (long r1, vnl_rational const &r2)
 
bool operator>= (int r1, vnl_rational const &r2)
 
bool operator>= (long r1, vnl_rational const &r2)
 
long truncate (vnl_rational const &r)
 
long floor (vnl_rational const &r)
 
long ceil (vnl_rational const &r)
 
long round (vnl_rational const &r)
 
vnl_rational vnl_math::abs (vnl_rational const &x)
 
vnl_rational vnl_math::squared_magnitude (vnl_rational const &x)
 
vnl_rational vnl_math::sqr (vnl_rational const &x)
 
bool vnl_math::isnan (vnl_rational const &)
 
bool vnl_math::isfinite (vnl_rational const &x)
 

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.

Author
Copyright (C) 1991 Texas Instruments Incorporated.

Permission is granted to any individual or institution to use, copy, modify, and distribute this software, provided that this complete copyright and permission notice is maintained, intact, in all copies and supporting documentation.

Texas Instruments Incorporated provides this software "as is" without express or implied warranty.

 Modifications
  Peter Vanroose, 13 July 2001: Added continued fraction cnstrctr from double
  Peter Vanroose, 10 July 2001: corrected operator%=()
  Peter Vanroose, 10 July 2001: corrected ceil() and floor() for negative args
  Peter Vanroose, 10 July 2001: extended operability range of += by using gcd
  Peter Vanroose, 10 July 2001: added abs().
  Peter Vanroose, 10 July 2001: removed state data member and added Inf repres
  Peter Vanroose,  9 July 2001: ported to vnl from COOL
  Peter Vanroose, 11 June 2009: made "*" and "/" robust against int overflow
                                (actually a full re-implementation, using gcd)

Definition in file vnl_rational.h.

Function Documentation

◆ ceil()

long ceil ( vnl_rational const &  r)
inline

Definition at line 469 of file vnl_rational.h.

◆ floor()

long floor ( vnl_rational const &  r)
inline

Definition at line 468 of file vnl_rational.h.

◆ operator *() [1/5]

vnl_rational operator * ( vnl_rational const &  r1,
vnl_rational const &  r2 
)
inline

Returns the product of two rational numbers.

Definition at line 375 of file vnl_rational.h.

◆ operator *() [2/5]

vnl_rational operator * ( vnl_rational const &  r1,
long  r2 
)
inline

Definition at line 380 of file vnl_rational.h.

◆ operator *() [3/5]

vnl_rational operator * ( vnl_rational const &  r1,
int  r2 
)
inline

Definition at line 385 of file vnl_rational.h.

◆ operator *() [4/5]

vnl_rational operator * ( long  r2,
vnl_rational const &  r1 
)
inline

Definition at line 390 of file vnl_rational.h.

◆ operator *() [5/5]

vnl_rational operator * ( int  r2,
vnl_rational const &  r1 
)
inline

Definition at line 395 of file vnl_rational.h.

◆ operator!=() [1/2]

bool operator!= ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 456 of file vnl_rational.h.

◆ operator!=() [2/2]

bool operator!= ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 457 of file vnl_rational.h.

◆ operator%() [1/5]

vnl_rational operator% ( vnl_rational const &  r1,
vnl_rational const &  r2 
)
inline

Returns the remainder of r1 divided by r2.

Definition at line 429 of file vnl_rational.h.

◆ operator%() [2/5]

vnl_rational operator% ( vnl_rational const &  r1,
long  r2 
)
inline

Definition at line 434 of file vnl_rational.h.

◆ operator%() [3/5]

vnl_rational operator% ( vnl_rational const &  r1,
int  r2 
)
inline

Definition at line 439 of file vnl_rational.h.

◆ operator%() [4/5]

vnl_rational operator% ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 444 of file vnl_rational.h.

◆ operator%() [5/5]

vnl_rational operator% ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 449 of file vnl_rational.h.

◆ operator+() [1/5]

vnl_rational operator+ ( vnl_rational const &  r1,
vnl_rational const &  r2 
)
inline

Returns the sum of two rational numbers.

Definition at line 321 of file vnl_rational.h.

◆ operator+() [2/5]

vnl_rational operator+ ( vnl_rational const &  r1,
long  r2 
)
inline

Definition at line 326 of file vnl_rational.h.

◆ operator+() [3/5]

vnl_rational operator+ ( vnl_rational const &  r1,
int  r2 
)
inline

Definition at line 331 of file vnl_rational.h.

◆ operator+() [4/5]

vnl_rational operator+ ( long  r2,
vnl_rational const &  r1 
)
inline

Definition at line 336 of file vnl_rational.h.

◆ operator+() [5/5]

vnl_rational operator+ ( int  r2,
vnl_rational const &  r1 
)
inline

Definition at line 341 of file vnl_rational.h.

◆ operator-() [1/5]

vnl_rational operator- ( vnl_rational const &  r1,
vnl_rational const &  r2 
)
inline

Returns the difference of two rational numbers.

Definition at line 348 of file vnl_rational.h.

◆ operator-() [2/5]

vnl_rational operator- ( vnl_rational const &  r1,
long  r2 
)
inline

Definition at line 353 of file vnl_rational.h.

◆ operator-() [3/5]

vnl_rational operator- ( vnl_rational const &  r1,
int  r2 
)
inline

Definition at line 358 of file vnl_rational.h.

◆ operator-() [4/5]

vnl_rational operator- ( long  r2,
vnl_rational const &  r1 
)
inline

Definition at line 363 of file vnl_rational.h.

◆ operator-() [5/5]

vnl_rational operator- ( int  r2,
vnl_rational const &  r1 
)
inline

Definition at line 368 of file vnl_rational.h.

◆ operator/() [1/5]

vnl_rational operator/ ( vnl_rational const &  r1,
vnl_rational const &  r2 
)
inline

Returns the quotient of two rational numbers.

Definition at line 402 of file vnl_rational.h.

◆ operator/() [2/5]

vnl_rational operator/ ( vnl_rational const &  r1,
long  r2 
)
inline

Definition at line 407 of file vnl_rational.h.

◆ operator/() [3/5]

vnl_rational operator/ ( vnl_rational const &  r1,
int  r2 
)
inline

Definition at line 412 of file vnl_rational.h.

◆ operator/() [4/5]

vnl_rational operator/ ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 417 of file vnl_rational.h.

◆ operator/() [5/5]

vnl_rational operator/ ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 422 of file vnl_rational.h.

◆ operator<() [1/2]

bool operator< ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 458 of file vnl_rational.h.

◆ operator<() [2/2]

bool operator< ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 459 of file vnl_rational.h.

◆ operator<<()

std::ostream & operator<< ( std::ostream &  s,
vnl_rational const &  r 
)
inline

formatted output.

Definition at line 306 of file vnl_rational.h.

◆ operator<=() [1/2]

bool operator<= ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 462 of file vnl_rational.h.

◆ operator<=() [2/2]

bool operator<= ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 463 of file vnl_rational.h.

◆ operator==() [1/2]

bool operator== ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 454 of file vnl_rational.h.

◆ operator==() [2/2]

bool operator== ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 455 of file vnl_rational.h.

◆ operator>() [1/2]

bool operator> ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 460 of file vnl_rational.h.

◆ operator>() [2/2]

bool operator> ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 461 of file vnl_rational.h.

◆ operator>=() [1/2]

bool operator>= ( int  r1,
vnl_rational const &  r2 
)
inline

Definition at line 464 of file vnl_rational.h.

◆ operator>=() [2/2]

bool operator>= ( long  r1,
vnl_rational const &  r2 
)
inline

Definition at line 465 of file vnl_rational.h.

◆ operator>>()

std::istream & operator>> ( std::istream &  s,
vnl_rational r 
)
inline

simple input.

Definition at line 313 of file vnl_rational.h.

◆ round()

long round ( vnl_rational const &  r)
inline

Definition at line 470 of file vnl_rational.h.

◆ truncate()

long truncate ( vnl_rational const &  r)
inline

Definition at line 467 of file vnl_rational.h.