Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GEO::rationalg< T > Class Template Reference

rationalg (generic rational) is used to compute the sign of rational fractions exactly. More...

#include <geogram/basic/rationalg.h>

Public Types

typedef T value_type
 

Public Member Functions

 rationalg (double x)
 Constructs a new rationalg from a double. More...
 
 rationalg (const T &x)
 Constructs a new rationalg from an T. More...
 
 rationalg (T &&x)
 Constructs a new rationalg from an T with move semantics. More...
 
 rationalg (double num, double denom)
 Constructs a new rationalg from two doubles. More...
 
 rationalg (const T &num, const T &denom)
 Constructs a new rationalg from two T. More...
 
 rationalg (T &&num, T &&denom)
 Constructs a new rationalg from two T with move semantics. More...
 
 rationalg (const rationalg< T > &rhs)=default
 Copy-constructor. More...
 
 rationalg (rationalg< T > &&rhs)=default
 Move-constructor. More...
 
rationalg< T > & operator= (const rationalg< T > &rhs)=default
 Assignment operator. More...
 
rationalg< T > & operator= (rationalg< T > &&rhs)=default
 Assignment operator with move semantics. More...
 
const T & num () const
 gets the numerator. More...
 
const T & denom () const
 gets the denominator. More...
 
T & num ()
 gets the numerator. More...
 
T & denom ()
 gets the denominator. More...
 
void optimize ()
 Optimizes the internal representation without changing the represented value.
 
rationalg< T > & operator+= (const rationalg< T > &rhs)
 Adds a rationalg to this rationalg. More...
 
rationalg< T > & operator-= (const rationalg< T > &rhs)
 Subtracts a rationalg to this rationalg. More...
 
rationalg< T > & operator*= (const rationalg< T > &rhs)
 Multiplies this rationalg by a rationalg. More...
 
rationalg< T > & operator/= (const rationalg< T > &rhs)
 Divides this rationalg by a rationalg. More...
 
rationalg< T > & operator+= (double rhs)
 Adds a double to this rationalg. More...
 
rationalg< T > & operator-= (double rhs)
 Subtracts a double from this rationalg. More...
 
rationalg< T > & operator*= (double rhs)
 Multiplies this rationalg by a double. More...
 
rationalg< T > & operator/= (double rhs)
 Divides this rationalg by a double. More...
 
rationalg< T > operator+ (const rationalg< T > &rhs) const
 Computes the sum of two rationalgs. More...
 
rationalg< T > operator- (const rationalg< T > &rhs) const
 Computes the difference between two rationalgs. More...
 
rationalg< T > operator* (const rationalg< T > &rhs) const
 Computes the product between two rationalgs. More...
 
rationalg< T > operator/ (const rationalg< T > &rhs) const
 Computes the ratio between two rationalgs. More...
 
rationalg< T > operator+ (double rhs) const
 Computes the sum of a rationalg and a double. More...
 
rationalg< T > operator- (double rhs) const
 Computes the difference between a rationalg and a double. More...
 
rationalg< T > operator* (double rhs) const
 Computes the product between a rationalg and a double. More...
 
rationalg< T > operator/ (double rhs) const
 Computes the ratio between a rationalg and a double. More...
 
rationalg< T > operator- () const
 Computes the opposite of this rationalg. More...
 
Sign sign () const
 Gets the sign of a rationalg. More...
 
Sign compare (const rationalg< T > &rhs) const
 Compares two rationalg. More...
 
Sign compare (double rhs) const
 Compares a rationalg with a double. More...
 
bool operator> (const rationalg< T > &rhs) const
 Compares this rationalg with another one. More...
 
bool operator>= (const rationalg< T > &rhs) const
 Compares this rationalg with another one. More...
 
bool operator< (const rationalg< T > &rhs) const
 Compares this rationalg with another one. More...
 
bool operator<= (const rationalg< T > &rhs) const
 Compares this rationalg with another one. More...
 
bool operator> (double rhs) const
 Compares this rationalg with another one. More...
 
bool operator>= (double rhs) const
 Compares this rationalg with another one. More...
 
bool operator< (double rhs) const
 Compares this rationalg with another one. More...
 
bool operator<= (double rhs) const
 Compares this rationalg with another one. More...
 
double estimate () const
 Computes an approximation of the stored value in this rational. More...
 

Protected Member Functions

void copy (const rationalg< T > &rhs)
 Copies a rational into this one. More...
 
bool has_same_denom (const rationalg< T > &rhs) const
 Tests whether a rationalg has trivially the same denominator this rationalg. More...
 

Related Functions

(Note that these are not member functions.)

template<class T >
rationalg< T > operator+ (double a, const rationalg< T > &b)
 Computes the sum of a double and a rationalg. More...
 
template<class T >
rationalg< T > operator- (double a, const rationalg< T > &b)
 Computes the difference between a double and a rationalg. More...
 
template<class T >
rationalg< T > operator* (double a, const rationalg< T > &b)
 Computes the product of a double and a rationalg. More...
 
template<class T >
rationalg< T > operator/ (double a, const rationalg< T > &b)
 Computes the ratio between a double and a rationalg. More...
 
template<class T >
bool operator== (const rationalg< T > &a, const rationalg< T > &b)
 Tests equality between two rationalgs. More...
 
template<class T >
bool operator== (const rationalg< T > &a, double b)
 Tests equality between a rationalg and a double. More...
 
template<class T >
bool operator== (double a, const rationalg< T > &b)
 Tests equality between a double and a rationalg. More...
 
template<class T >
bool operator!= (const rationalg< T > &a, const rationalg< T > &b)
 Tests whether two rationalgs differ. More...
 
template<class T >
bool operator!= (const rationalg< T > &a, double b)
 Tests whether a rationalg differs from a double. More...
 
template<class T >
bool operator!= (double a, const rationalg< T > &b)
 Tests whether a double differs from a rationalg. More...
 

Detailed Description

template<class T>
class GEO::rationalg< T >

rationalg (generic rational) is used to compute the sign of rational fractions exactly.

rationalg can be used like float and double. It supports four arithmetic operations (+,-,*,/), comparisons (>,>=,<,<=,==,!=) and exact sign computation.

Definition at line 59 of file rationalg.h.

Constructor & Destructor Documentation

◆ rationalg() [1/8]

template<class T >
GEO::rationalg< T >::rationalg ( double  x)
inlineexplicit

Constructs a new rationalg from a double.

Parameters
[in]xthe value to initialize this rationalg.

Definition at line 69 of file rationalg.h.

◆ rationalg() [2/8]

template<class T >
GEO::rationalg< T >::rationalg ( const T &  x)
inlineexplicit

Constructs a new rationalg from an T.

Parameters
[in]xthe value to initialize this rationalg.

Definition at line 76 of file rationalg.h.

◆ rationalg() [3/8]

template<class T >
GEO::rationalg< T >::rationalg ( T &&  x)
inlineexplicit

Constructs a new rationalg from an T with move semantics.

Parameters
[in]xthe victim T

Definition at line 84 of file rationalg.h.

◆ rationalg() [4/8]

template<class T >
GEO::rationalg< T >::rationalg ( double  num,
double  denom 
)
inlineexplicit

Constructs a new rationalg from two doubles.

Parameters
[in]numthe numerator
[in]denomthe denominator

Definition at line 92 of file rationalg.h.

◆ rationalg() [5/8]

template<class T >
GEO::rationalg< T >::rationalg ( const T &  num,
const T &  denom 
)
inlineexplicit

Constructs a new rationalg from two T.

Parameters
[in]numthe numerator
[in]denomthe denominator

Definition at line 101 of file rationalg.h.

◆ rationalg() [6/8]

template<class T >
GEO::rationalg< T >::rationalg ( T &&  num,
T &&  denom 
)
inlineexplicit

Constructs a new rationalg from two T with move semantics.

Parameters
[in]numthe numerator
[in]denomthe denominator

Definition at line 111 of file rationalg.h.

◆ rationalg() [7/8]

template<class T >
GEO::rationalg< T >::rationalg ( const rationalg< T > &  rhs)
default

Copy-constructor.

Parameters
[in]rhsthe rational to be copied

◆ rationalg() [8/8]

template<class T >
GEO::rationalg< T >::rationalg ( rationalg< T > &&  rhs)
default

Move-constructor.

Parameters
[in]rhsthe rational to be copied

Member Function Documentation

◆ compare() [1/2]

template<class T >
Sign GEO::rationalg< T >::compare ( const rationalg< T > &  rhs) const
inline

Compares two rationalg.

Returns
the sign of this rationalg minus rhs

Definition at line 427 of file rationalg.h.

◆ compare() [2/2]

template<class T >
Sign GEO::rationalg< T >::compare ( double  rhs) const
inline

Compares a rationalg with a double.

Returns
the sign of this expansion minus rhs

Definition at line 444 of file rationalg.h.

◆ copy()

template<class T >
void GEO::rationalg< T >::copy ( const rationalg< T > &  rhs)
inlineprotected

Copies a rational into this one.

Parameters
[in]rhsa const reference to the rational to be copied

Definition at line 554 of file rationalg.h.

◆ denom() [1/2]

template<class T >
T& GEO::rationalg< T >::denom ( )
inline

gets the denominator.

Returns
a reference to the denominator.

Definition at line 170 of file rationalg.h.

◆ denom() [2/2]

template<class T >
const T& GEO::rationalg< T >::denom ( ) const
inline

gets the denominator.

Returns
a const reference to the denominator.

Definition at line 154 of file rationalg.h.

◆ estimate()

template<class T >
double GEO::rationalg< T >::estimate ( ) const
inline

Computes an approximation of the stored value in this rational.

Returns
an approximation of the stored value.

Definition at line 545 of file rationalg.h.

◆ has_same_denom()

template<class T >
bool GEO::rationalg< T >::has_same_denom ( const rationalg< T > &  rhs) const
inlineprotected

Tests whether a rationalg has trivially the same denominator this rationalg.

This function is used to implement faster addition, subtraction and tests when it can be quickly determined that both operands have the same denominator.

Return values
trueif it is trivial that rhs has the same denominator as this rationalg.
falseotherwise.

Definition at line 569 of file rationalg.h.

◆ num() [1/2]

template<class T >
T& GEO::rationalg< T >::num ( )
inline

gets the numerator.

Returns
a reference to the numerator.

Definition at line 162 of file rationalg.h.

◆ num() [2/2]

template<class T >
const T& GEO::rationalg< T >::num ( ) const
inline

gets the numerator.

Returns
a const reference to the numerator.

Definition at line 146 of file rationalg.h.

◆ operator*() [1/2]

template<class T >
rationalg<T> GEO::rationalg< T >::operator* ( const rationalg< T > &  rhs) const
inline

Computes the product between two rationalgs.

Parameters
[in]rhsthe rationalg to be multiplied by this rationalg
Returns
the product between this rationalg and rhs

Definition at line 328 of file rationalg.h.

◆ operator*() [2/2]

template<class T >
rationalg<T> GEO::rationalg< T >::operator* ( double  rhs) const
inline

Computes the product between a rationalg and a double.

Parameters
[in]rhsthe double to be multiplied by this rationalg
Returns
the product between this rationalg and rhs

Definition at line 378 of file rationalg.h.

◆ operator*=() [1/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator*= ( const rationalg< T > &  rhs)
inline

Multiplies this rationalg by a rationalg.

Parameters
[in]rhsthe rationalg to multiply this rationalg by
Returns
the new value of this rationalg

Definition at line 220 of file rationalg.h.

◆ operator*=() [2/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator*= ( double  rhs)
inline

Multiplies this rationalg by a double.

If the double is a constant (possibly negative) power of two (e.g. 0.125, 0.5, 2.0, 4.0 ...), one may use num().scale_fast() / denom().scale_fast() instead.

Parameters
[in]rhsthe double to multiply this rationalg with
Returns
the new value of this rationalg

Definition at line 265 of file rationalg.h.

◆ operator+() [1/2]

template<class T >
rationalg<T> GEO::rationalg< T >::operator+ ( const rationalg< T > &  rhs) const
inline

Computes the sum of two rationalgs.

Parameters
[in]rhsthe rationalg to be added to this rationalg
Returns
the sum of this rationalg and rhs

Definition at line 290 of file rationalg.h.

◆ operator+() [2/2]

template<class T >
rationalg<T> GEO::rationalg< T >::operator+ ( double  rhs) const
inline

Computes the sum of a rationalg and a double.

Parameters
[in]rhsthe double to be added to this rationalg
Returns
the sum of this rationalg and rhs

Definition at line 354 of file rationalg.h.

◆ operator+=() [1/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator+= ( const rationalg< T > &  rhs)
inline

Adds a rationalg to this rationalg.

Parameters
[in]rhsthe rationalg to be added to this rationalg
Returns
the new value of this rationalg

Definition at line 190 of file rationalg.h.

◆ operator+=() [2/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator+= ( double  rhs)
inline

Adds a double to this rationalg.

Parameters
[in]rhsthe double to be added to this rationalg
Returns
the new value of this rationalg

Definition at line 242 of file rationalg.h.

◆ operator-() [1/3]

template<class T >
rationalg<T> GEO::rationalg< T >::operator- ( ) const
inline

Computes the opposite of this rationalg.

Returns
the opposite of this rationalg

Definition at line 403 of file rationalg.h.

◆ operator-() [2/3]

template<class T >
rationalg<T> GEO::rationalg< T >::operator- ( const rationalg< T > &  rhs) const
inline

Computes the difference between two rationalgs.

Parameters
[in]rhsthe rationalg to be subtracted from this rationalg
Returns
the difference between this rationalg and rhs

Definition at line 309 of file rationalg.h.

◆ operator-() [3/3]

template<class T >
rationalg<T> GEO::rationalg< T >::operator- ( double  rhs) const
inline

Computes the difference between a rationalg and a double.

Parameters
[in]rhsthe double to be subtracted from this rationalg
Returns
the difference between this rationalg and rhs

Definition at line 366 of file rationalg.h.

◆ operator-=() [1/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator-= ( const rationalg< T > &  rhs)
inline

Subtracts a rationalg to this rationalg.

Parameters
[in]rhsthe rationalg to be subtracted
Returns
the new value of this rationalg

Definition at line 205 of file rationalg.h.

◆ operator-=() [2/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator-= ( double  rhs)
inline

Subtracts a double from this rationalg.

Parameters
[in]rhsthe double to be subtracted from this rationalg
Returns
the new value of this rationalg

Definition at line 252 of file rationalg.h.

◆ operator/() [1/2]

template<class T >
rationalg<T> GEO::rationalg< T >::operator/ ( const rationalg< T > &  rhs) const
inline

Computes the ratio between two rationalgs.

Parameters
[in]rhsthe rationalg to be multiplied by this rationalg
Returns
the ratio between this rationalg and rhs

Definition at line 341 of file rationalg.h.

◆ operator/() [2/2]

template<class T >
rationalg<T> GEO::rationalg< T >::operator/ ( double  rhs) const
inline

Computes the ratio between a rationalg and a double.

Parameters
[in]rhsthe double to be multiplied by this rationalg
Returns
the ratio between this rationalg and rhs

Definition at line 390 of file rationalg.h.

◆ operator/=() [1/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator/= ( const rationalg< T > &  rhs)
inline

Divides this rationalg by a rationalg.

Parameters
[in]rhsthe rationalg to divide this rationalg by
Returns
the new value of this rationalg

Definition at line 231 of file rationalg.h.

◆ operator/=() [2/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator/= ( double  rhs)
inline

Divides this rationalg by a double.

If the double is a constant (possibly negative) power of two (e.g. 0.125, 0.5, 2.0, 4.0 ...), one may use num().scale_fast() / denom().scale_fast() instead.

Parameters
[in]rhsthe double to multiply this rationalg with
Returns
the new value of this rationalg

Definition at line 278 of file rationalg.h.

◆ operator<() [1/2]

template<class T >
bool GEO::rationalg< T >::operator< ( const rationalg< T > &  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is smaller than rhs, false otherwise

Definition at line 479 of file rationalg.h.

◆ operator<() [2/2]

template<class T >
bool GEO::rationalg< T >::operator< ( double  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is smaller than rhs, false otherwise

Definition at line 523 of file rationalg.h.

◆ operator<=() [1/2]

template<class T >
bool GEO::rationalg< T >::operator<= ( const rationalg< T > &  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is smaller or equal than rhs, false otherwise

Definition at line 490 of file rationalg.h.

◆ operator<=() [2/2]

template<class T >
bool GEO::rationalg< T >::operator<= ( double  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is smaller or equal than rhs, false otherwise

Definition at line 534 of file rationalg.h.

◆ operator=() [1/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator= ( const rationalg< T > &  rhs)
default

Assignment operator.

Parameters
[in]rhsthe rational to be copied
Returns
the new value of this rational (rhs)

◆ operator=() [2/2]

template<class T >
rationalg<T>& GEO::rationalg< T >::operator= ( rationalg< T > &&  rhs)
default

Assignment operator with move semantics.

Parameters
[in]rhsthe victim rationalg
Returns
the new value of this rational (rhs)

◆ operator>() [1/2]

template<class T >
bool GEO::rationalg< T >::operator> ( const rationalg< T > &  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is greater than rhs, false otherwise

Definition at line 457 of file rationalg.h.

◆ operator>() [2/2]

template<class T >
bool GEO::rationalg< T >::operator> ( double  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is greater than rhs, false otherwise

Definition at line 501 of file rationalg.h.

◆ operator>=() [1/2]

template<class T >
bool GEO::rationalg< T >::operator>= ( const rationalg< T > &  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is greater or equal than rhs, false otherwise

Definition at line 468 of file rationalg.h.

◆ operator>=() [2/2]

template<class T >
bool GEO::rationalg< T >::operator>= ( double  rhs) const
inline

Compares this rationalg with another one.

Internally computes the sign of the difference between this rationalg and rhs.

Returns
true if this rationalg is greater or equal than rhs, false otherwise

Definition at line 512 of file rationalg.h.

◆ sign()

template<class T >
Sign GEO::rationalg< T >::sign ( ) const
inline

Gets the sign of a rationalg.

Returns
one of POSITIVE,ZERO,NEGATIVE

Definition at line 416 of file rationalg.h.

Friends And Related Function Documentation

◆ operator!=() [1/3]

template<class T >
bool operator!= ( const rationalg< T > &  a,
const rationalg< T > &  b 
)
related

Tests whether two rationalgs differ.

Implemented by testing whether the difference between a and b is different from 0.

Returns
true if a and b do not represent the same exact value, false otherwise

Definition at line 681 of file rationalg.h.

◆ operator!=() [2/3]

template<class T >
bool operator!= ( const rationalg< T > &  a,
double  b 
)
related

Tests whether a rationalg differs from a double.

Implemented by testing whether the difference between a and b is different from 0.

Returns
true if a and b do not represent the same exact value, false otherwise

Definition at line 694 of file rationalg.h.

◆ operator!=() [3/3]

template<class T >
bool operator!= ( double  a,
const rationalg< T > &  b 
)
related

Tests whether a double differs from a rationalg.

Implemented by testing whether the difference between a and b is different from 0.

Returns
true if a and b do not represent the same exact value, false otherwise

Definition at line 707 of file rationalg.h.

◆ operator*()

template<class T >
rationalg< T > operator* ( double  a,
const rationalg< T > &  b 
)
related

Computes the product of a double and a rationalg.

Parameters
[in]athe double
[in]bthe rationalg to be multiplied
Returns
a rationalg that represents a * b

Definition at line 614 of file rationalg.h.

◆ operator+()

template<class T >
rationalg< T > operator+ ( double  a,
const rationalg< T > &  b 
)
related

Computes the sum of a double and a rationalg.

Parameters
[in]athe double to be added
[in]bthe rationalg to be added
Returns
a rationalg that represents a + b

Definition at line 588 of file rationalg.h.

◆ operator-()

template<class T >
rationalg< T > operator- ( double  a,
const rationalg< T > &  b 
)
related

Computes the difference between a double and a rationalg.

Parameters
[in]athe double
[in]bthe rationalg to be subtracted
Returns
a rationalg that represents a - b

Definition at line 600 of file rationalg.h.

◆ operator/()

template<class T >
rationalg< T > operator/ ( double  a,
const rationalg< T > &  b 
)
related

Computes the ratio between a double and a rationalg.

Parameters
[in]athe double
[in]bthe rationalg to be divided
Returns
a rationalg that represents a / b

Definition at line 626 of file rationalg.h.

◆ operator==() [1/3]

template<class T >
bool operator== ( const rationalg< T > &  a,
const rationalg< T > &  b 
)
related

Tests equality between two rationalgs.

Implemented by testing whether the difference between a and b is 0.

Returns
true if a and b represent exactly the same value, false otherwise

Definition at line 642 of file rationalg.h.

◆ operator==() [2/3]

template<class T >
bool operator== ( const rationalg< T > &  a,
double  b 
)
related

Tests equality between a rationalg and a double.

Implemented by testing whether the difference between a and b is 0.

Returns
true if a and b represent exactly the same value, false otherwise

Definition at line 655 of file rationalg.h.

◆ operator==() [3/3]

template<class T >
bool operator== ( double  a,
const rationalg< T > &  b 
)
related

Tests equality between a double and a rationalg.

Implemented by testing whether the difference between a and b is 0.

Returns
true if a and b represent exactly the same value, false otherwise

Definition at line 668 of file rationalg.h.


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