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

Rational< IntType > Class Template Reference

`#include <vigra/rational.hxx>`

## Public Types

typedef If< typenameTypeTraits< IntType >::isBuiltinType, IntType, IntTypeconst & >::type | param_type
typedef IntType | value_type

## Public Member Functions

Rational & | assign (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
Rational & | operator*= (const Rational &r)
Rational & | operator*= (param_type i)
Rational & | operator++ ()
Rational | operator++ (int)
Rational & | operator+= (const Rational &r)
Rational & | operator+= (param_type i)
Rational & | operator-- ()
Rational | operator-- (int)
Rational & | operator-= (const Rational &r)
Rational & | operator-= (param_type i)
Rational & | operator/= (const Rational &r)
Rational & | operator/= (param_type i)
Rational & | operator= (param_type n)
| Rational ()
| Rational (double v, double epsilon=1e-4)
| Rational (param_type n)
| Rational (param_type n, param_type d, bool doNormalize=true)
template<class U >
| Rational (Rational< U > const &r)
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:

- Numeric and Promote Traits of Rational
- Functions for Rational

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

## Member Typedef Documentation

## ◆ value_type

template<typename IntType >

typedef IntType value_type

The type of numerator and denominator

## ◆ param_type

template<typename IntType >

typedef If<typenameTypeTraits<IntType>::isBuiltinType,IntType,IntTypeconst&>::type param_type

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

## Constructor & Destructor Documentation

## ◆ Rational() [1/5]

template<typename IntType >

Rational | ( |  | ) |

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

## ◆ Rational() [2/5]

template<typename IntType >
template<class U >

Rational | ( | Rational< U > const & | r | ) |

Copy constructor

## ◆ Rational() [3/5]

template<typename IntType >

Rational | ( | param_type | n | ) |

Integer constructor: creates `n/1`

## ◆ Rational() [4/5]

template<typename IntType >

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() [5/5]

template<typename IntType >

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

## ◆ operator=()

template<typename IntType >

Rational & operator= | ( | param_type | n | ) |

Assignment from `IntType`.

## ◆ assign()

template<typename IntType >

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

Assignment from `IntType` pair.

## ◆ numerator()

template<typename IntType >

param_type numerator | ( |  | ) | const

Access numerator.

## ◆ denominator()

template<typename IntType >

param_type denominator | ( |  | ) | const

Access denominator.

## ◆ operator+=() [1/2]

template<typename IntType >

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

Add-assignment from `Rational`

`throws bad_rational` if indeterminate expression.

## ◆ operator-=() [1/2]

template<typename IntType >

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

Subtract-assignment from `Rational`

`throws bad_rational` if indeterminate expression.

## ◆ operator*=() [1/2]

template<typename IntType >

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

Multiply-assignment from `Rational`

`throws bad_rational` if indeterminate expression.

## ◆ operator/=() [1/2]

template<typename IntType >

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

Divide-assignment from `Rational`

`throws bad_rational` if indeterminate expression.

## ◆ operator+=() [2/2]

template<typename IntType >

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

Add-assignment from `IntType`

`throws bad_rational` if indeterminate expression.

## ◆ operator-=() [2/2]

template<typename IntType >

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

Subtract-assignment from `IntType`

`throws bad_rational` if indeterminate expression.

## ◆ operator*=() [2/2]

template<typename IntType >

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

Multiply-assignment from `IntType`

`throws bad_rational` if indeterminate expression.

## ◆ operator/=() [2/2]

template<typename IntType >

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

Divide-assignment from `IntType`

`throws bad_rational` if indeterminate expression.

## ◆ operator++() [1/2]

template<typename IntType >

Rational< IntType > & operator++ | ( |  | ) |

Pre-increment.

## ◆ operator--() [1/2]

template<typename IntType >

Rational< IntType > & operator-- | ( |  | ) |

Pre-decrement.

## ◆ operator++() [2/2]

template<typename IntType >

Rational operator++ | ( | int |  | ) |

Post-increment.

## ◆ operator--() [2/2]

template<typename IntType >

Rational operator-- | ( | int |  | ) |

Post-decrement.

## ◆ operator!()

template<typename IntType >

bool operator! | ( |  | ) | const

Check for zero by calling `!numerator()`

## ◆ is_pinf()

template<typename IntType >

bool is_pinf | ( |  | ) | const

Check whether we have positive infinity.

## ◆ is_ninf()

template<typename IntType >

bool is_ninf | ( |  | ) | const

Check whether we have negative infinity.

## ◆ is_inf()

template<typename IntType >

bool is_inf | ( |  | ) | const

Check whether we have positive or negative infinity.

## ◆ sign()

template<typename IntType >

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:

- vigra/rational.hxx

© 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.12.1