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

| Rational Class Template Reference |
--- | --- | ---

`#include "vigra/rational.hxx"`

## Public Types
typedef IntType | value_type
typedef If< typename TypeTraits< IntType >::isBuiltinType, IntType, IntType const & >::type | param_type
## Public Methods
| 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)
Rational & | operator= (param_type n)
Rational & | assign (param_type n, param_type d, bool doNormalize=true)
param_type | numerator () const
param_type | denominator () const
Rational & | operator+= (const Rational &r)
Rational & | operator-= (const Rational &r)
Rational & | operator *= (const Rational &r)
Rational & | operator/= (const Rational &r)
Rational & | operator+= (param_type i)
Rational & | operator-= (param_type i)
Rational & | operator *= (param_type i)
Rational & | operator/= (param_type i)
Rational & | operator++ ()
Rational & | operator-- ()
Rational | operator++ (int)
Rational | operator-- (int)
bool | operator! () const
bool | is_pinf () const
bool | is_ninf () const
bool | is_inf () const
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 and promotion traits". All arithmetic and comparison operators, as well as the relevant algebraic functions are supported .

See also:

- Numeric and Promote Traits of Rational
- Functions for Rational

#include "vigra/rational.hxx"
Namespace: vigra

## Member Typedef Documentation

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

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

typedef IntType value_type

| The type of numerator and denominator

## Constructor & Destructor Documentation

Rational | ( |  | ) | `[inline]`

| Default constructor: creates zero (`0/1`)

Rational | ( | Rational< U > const & | r | ) | `[inline]`

| Copy constructor

Rational | ( | param_type | n | ) | `[inline]`

| Integer constructor: creates `n/1`

Rational | ( | param_type | n,
|  | param_type | d,
|  | bool | doNormalize = true
| ) | `[inline]`

| 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
| ) | `[inline, explicit]`

| Construct as an approximation of a real number. The maximal allowed relative error is given by `epsilon`.

## Member Function Documentation

Rational< IntType > & assign | ( | param_type | n,
|  | param_type | d,
|  | bool | doNormalize = true
| ) | `[inline]`

| Assignment from `IntType` pair.

param_type denominator | ( |  | ) | const`[inline]`

| Access denominator.

bool is_inf | ( |  | ) | const`[inline]`

| Check whether we have positive or negative infinity.

bool is_ninf | ( |  | ) | const`[inline]`

| Check whether we have negative infinity.

bool is_pinf | ( |  | ) | const`[inline]`

| Check whether we have positive infinity.

param_type numerator | ( |  | ) | const`[inline]`

| Access numerator.

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

| Multiply-assignment from `IntType` `throws bad_rational` if indeterminate expression.

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

| Multiply-assignment from `Rational` `throws bad_rational` if indeterminate expression.

bool operator! | ( |  | ) | const`[inline]`

| Check for zero by calling `!numerator()`

Rational operator++ | ( | int |  | ) | `[inline]`

| Post-increment.

Rational< IntType > & operator++ | ( |  | ) | `[inline]`

| Pre-increment.

Rational< IntType > & operator+= | ( | param_type | i | ) | `[inline]`

| Add-assignment from `IntType` `throws bad_rational` if indeterminate expression.

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

| Add-assignment from `Rational` `throws bad_rational` if indeterminate expression.

Rational operator-- | ( | int |  | ) | `[inline]`

| Post-decrement.

Rational< IntType > & operator-- | ( |  | ) | `[inline]`

| Pre-decrement.

Rational< IntType > & operator-= | ( | param_type | i | ) | `[inline]`

| Subtract-assignment from `IntType` `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/= | ( | param_type | i | ) |

| Divide-assignment from `IntType` `throws bad_rational` if indeterminate expression.

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

| Divide-assignment from `Rational` `throws bad_rational` if indeterminate expression.

Rational& operator= | ( | param_type | n | ) | `[inline]`

| Assignment from `IntType`.

int sign | ( |  | ) | const`[inline]`

| 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:

- vigra/rational.hxx

© Ullrich Köthe (koethe@informatik.uni-hamburg.de) Cognitive Systems Group, University of Hamburg, Germany | html generated using doxygen and Python VIGRA 1.3.2 (27 Jan 2005)