[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

details Rational< IntType > Class Template Reference VIGRA

#include <vigra/rational.hxx>

Public Types

typedef If< typename
TypeTraits< IntType >
::isBuiltinType, IntType,
IntType const & >::type 
param_type
 
typedef IntType value_type
 

Public Member Functions

Rationalassign (param_type n, param_type d, bool doNormalize=true)
 
param_type denominator () const
 
bool is_inf () const
 
bool is_ninf () const
 
bool is_pinf () const
 
param_type numerator () const
 
bool operator! () const
 
Rationaloperator*= (const Rational &r)
 
Rationaloperator*= (param_type i)
 
Rationaloperator++ ()
 
Rational operator++ (int)
 
Rationaloperator+= (const Rational &r)
 
Rationaloperator+= (param_type i)
 
Rationaloperator-- ()
 
Rational operator-- (int)
 
Rationaloperator-= (const Rational &r)
 
Rationaloperator-= (param_type i)
 
Rationaloperator/= (const Rational &r)
 
Rationaloperator/= (param_type i)
 
Rationaloperator= (param_type n)
 
 Rational ()
 
template<class U >
 Rational (Rational< U > const &r)
 
 Rational (param_type n)
 
 Rational (param_type n, param_type d, bool doNormalize=true)
 
 Rational (double v, double epsilon=1e-4)
 
int sign () const
 

Detailed Description

template<typename IntType>
class vigra::Rational< IntType >

Template for rational numbers.

This template can make use of arbitrary integer types, including user-defined (e.g. infinite precision) ones. Note, however, that overflow in either the numerator or denominator is not detected during calculations – the standard behavior of the integer type (e.g. wrap around) applies.

The class can represent and handle positive and negative infinity resulting from division by zero. Indeterminate expressions such as 0/0 are signaled by a bad_rational exception which is derived from std::domain_error.

Rational implements the required interface of an AlgebraicField and the required numeric andpromotion traits". All arithmetic and comparison operators, as well as the relevant algebraic functions are supported .

See also:

#include <vigra/rational.hxx>
Namespace: vigra

Member Typedef Documentation

typedef IntType value_type

The type of numerator and denominator

typedef If<typename TypeTraits<IntType>::isBuiltinType, IntType, IntType const &>::type param_type

Determine whether arguments should be passed as IntType or IntType const &.

Constructor & Destructor Documentation

Rational ( )

Default constructor: creates zero (0/1)

Rational ( Rational< U > const &  r)

Copy constructor

Integer constructor: creates n/1

Rational ( param_type  n,
param_type  d,
bool  doNormalize = true 
)

Ratio constructor: creates n/d.

The ratio will be normalized unless doNormalize = false. Since the internal representation is assumed to be normalized, doNormalize = false must only be used as an optimization if n and d are known to be already normalized (i.e. have 1 as their greatest common divisor).

Rational ( double  v,
double  epsilon = 1e-4 
)
explicit

Construct as an approximation of a real number.

The maximal allowed relative error is given by epsilon.

Member Function Documentation

Rational& operator= ( param_type  n)

Assignment from IntType.

Rational< IntType > & assign ( param_type  n,
param_type  d,
bool  doNormalize = true 
)

Assignment from IntType pair.

param_type numerator ( ) const

Access numerator.

param_type denominator ( ) const

Access denominator.

Rational< IntType > & operator+= ( const Rational< IntType > &  r)

Add-assignment from Rational

throws bad_rational if indeterminate expression.

Rational< IntType > & operator-= ( const Rational< IntType > &  r)

Subtract-assignment from Rational

throws bad_rational if indeterminate expression.

Rational< IntType > & operator*= ( const Rational< IntType > &  r)

Multiply-assignment from Rational

throws bad_rational if indeterminate expression.

Rational< IntType > & operator/= ( const Rational< IntType > &  r)

Divide-assignment from Rational

throws bad_rational if indeterminate expression.

Rational< IntType > & operator+= ( param_type  i)

Add-assignment from IntType

throws bad_rational if indeterminate expression.

Rational< IntType > & operator-= ( param_type  i)

Subtract-assignment from IntType

throws bad_rational if indeterminate expression.

Rational< IntType > & operator*= ( param_type  i)

Multiply-assignment from IntType

throws bad_rational if indeterminate expression.

Rational< IntType > & operator/= ( param_type  i)

Divide-assignment from IntType

throws bad_rational if indeterminate expression.

Rational< IntType > & operator++ ( )

Pre-increment.

Rational< IntType > & operator-- ( )

Pre-decrement.

Rational operator++ ( int  )

Post-increment.

Rational operator-- ( int  )

Post-decrement.

bool operator! ( ) const

Check for zero by calling !numerator()

bool is_pinf ( ) const

Check whether we have positive infinity.

bool is_ninf ( ) const

Check whether we have negative infinity.

bool is_inf ( ) const

Check whether we have positive or negative infinity.

int sign ( ) const

Check the sign.

Gives 1 if the number is positive, -1 if negative, and 0 otherwise.


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

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.11.1 (Fri May 19 2017)