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

Protected Member Functions

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

Related Symbols

(Note that these are not member symbols.)

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

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 60 of file rationalg.h.

Member Typedef Documentation

◆ value_type

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

Definition at line 62 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 70 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 77 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 85 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 93 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 102 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 112 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 428 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 445 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 555 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 171 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 155 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 546 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 570 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 163 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 147 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 329 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 379 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 221 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 266 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 291 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 355 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 191 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 243 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 404 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 310 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 367 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 206 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 253 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 342 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 391 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 232 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 279 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 480 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 524 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 491 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 535 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 458 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 502 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 469 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 513 of file rationalg.h.

◆ optimize()

template<class T >
void GEO::rationalg< T >::optimize ( )
inline

Optimizes the internal representation without changing the represented value.

Definition at line 179 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 417 of file rationalg.h.

Friends And Related Symbol 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 682 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 695 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 708 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 615 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 589 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 601 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 627 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 643 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 656 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 669 of file rationalg.h.


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