Geogram Version 1.8.5
A programming library of geometric algorithms
Loading...
Searching...
No Matches
GEO::rational_nt Class Reference

Rational_nt (rational Number Type) is used to compute the sign of rational fractions exactly. More...

#include <geogram/numerics/expansion_nt.h>

Public Types

enum  UninitializedType { UNINITIALIZED }
 This type is used to overload expression_nt constructors with a version that does not create an expansion. More...
 

Public Member Functions

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

Protected Member Functions

void copy (const rational_nt &rhs)
 Copies a rational into this one.
 
bool has_same_denom (const rational_nt &rhs) const
 Tests whether a rational_nt has trivially the same denominator as this rational_nt.
 

Related Symbols

(Note that these are not member symbols.)

rational_nt operator+ (double a, const rational_nt &b)
 Computes the sum of a double and a rational_nt.
 
rational_nt operator- (double a, const rational_nt &b)
 Computes the difference between a double and a rational_nt.
 
rational_nt operator* (double a, const rational_nt &b)
 Computes the product of a double and a rational_nt.
 
rational_nt operator/ (double a, const rational_nt &b)
 Computes the ratio between a double and a rational_nt.
 
bool operator== (const rational_nt &a, const rational_nt &b)
 Tests equality between two rational_nts.
 
bool operator== (const rational_nt &a, double b)
 Tests equality between a rational_nt and a double.
 
bool operator== (double a, const rational_nt &b)
 Tests equality between a double and a rational_nt.
 
bool operator!= (const rational_nt &a, const rational_nt &b)
 Tests whether two rational_nts differ.
 
bool operator!= (const rational_nt &a, double b)
 Tests whether a rational_nt differs from a double.
 
bool operator!= (double a, const rational_nt &b)
 Tests whether a double differs from a rational_nt.
 

Detailed Description

Rational_nt (rational Number Type) is used to compute the sign of rational fractions exactly.

Rational_nt can be used like float and double. It supports four arithmetic operations (+,-,*,/), comparisons (>,>=,<,<=,==,!=) and exact sign computation. When performance is a concern, the lower-level expansion class may be used instead.

Definition at line 1030 of file expansion_nt.h.

Member Enumeration Documentation

◆ UninitializedType

This type is used to overload expression_nt constructors with a version that does not create an expansion.

Definition at line 1038 of file expansion_nt.h.

Constructor & Destructor Documentation

◆ rational_nt() [1/9]

GEO::rational_nt::rational_nt ( UninitializedType  uninitialized)
inlineexplicit

Constructs an uninitialized rational_nt.

Definition at line 1045 of file expansion_nt.h.

◆ rational_nt() [2/9]

GEO::rational_nt::rational_nt ( double  x = 0.0)
inlineexplicit

Constructs a new rational_nt from a double.

Parameters
[in]xthe value to initialize this rational_nt.

Definition at line 1056 of file expansion_nt.h.

◆ rational_nt() [3/9]

GEO::rational_nt::rational_nt ( const expansion_nt x)
inlineexplicit

Constructs a new rational_nt from an expansion_nt.

Parameters
[in]xthe value to initialize this rational_nt.

Definition at line 1063 of file expansion_nt.h.

◆ rational_nt() [4/9]

GEO::rational_nt::rational_nt ( expansion_nt &&  x)
inlineexplicit

Constructs a new rational_nt from an expansion_nt with move semantics.

Parameters
[in]xthe victim expansion_nt

Definition at line 1071 of file expansion_nt.h.

◆ rational_nt() [5/9]

GEO::rational_nt::rational_nt ( double  num,
double  denom 
)
inlineexplicit

Constructs a new rational_nt from two doubles.

Parameters
[in]numthe numerator
[in]denomthe denominator

Definition at line 1079 of file expansion_nt.h.

◆ rational_nt() [6/9]

GEO::rational_nt::rational_nt ( const expansion_nt num,
const expansion_nt denom 
)
inlineexplicit

Constructs a new rational_nt from two expansion_nt.

Parameters
[in]numthe numerator
[in]denomthe denominator

Definition at line 1088 of file expansion_nt.h.

◆ rational_nt() [7/9]

GEO::rational_nt::rational_nt ( expansion_nt &&  num,
expansion_nt &&  denom 
)
inlineexplicit

Constructs a new rational_nt from two expansion_nt with move semantics.

Parameters
[in]numthe numerator
[in]denomthe denominator

Definition at line 1098 of file expansion_nt.h.

◆ rational_nt() [8/9]

GEO::rational_nt::rational_nt ( const rational_nt rhs)
inline

Copy-constructor.

Parameters
[in]rhsthe rational to be copied

Definition at line 1107 of file expansion_nt.h.

◆ rational_nt() [9/9]

GEO::rational_nt::rational_nt ( rational_nt &&  rhs)
inline

Move-constructor.

Parameters
[in]rhsthe rational to be copied

Definition at line 1115 of file expansion_nt.h.

Member Function Documentation

◆ compare() [1/2]

Sign GEO::rational_nt::compare ( const rational_nt rhs) const

Compares two rational_nt.

Returns
the sign of this expansion minus rhs

◆ compare() [2/2]

Sign GEO::rational_nt::compare ( double  rhs) const

Compares a rational_nt with a double.

Returns
the sign of this expansion minus rhs

◆ copy()

void GEO::rational_nt::copy ( const rational_nt rhs)
inlineprotected

Copies a rational into this one.

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

Definition at line 1538 of file expansion_nt.h.

◆ denom() [1/2]

expansion_nt & GEO::rational_nt::denom ( )
inline

gets the denominator.

Returns
a reference to the denominator.

Definition at line 1170 of file expansion_nt.h.

◆ denom() [2/2]

const expansion_nt & GEO::rational_nt::denom ( ) const
inline

gets the denominator.

Returns
a const reference to the denominator.

Definition at line 1154 of file expansion_nt.h.

◆ estimate()

double GEO::rational_nt::estimate ( ) const
inline

Computes an approximation of the stored value in this rational.

Returns
an approximation of the stored value.

Definition at line 1520 of file expansion_nt.h.

◆ has_same_denom()

bool GEO::rational_nt::has_same_denom ( const rational_nt rhs) const
inlineprotected

Tests whether a rational_nt has trivially the same denominator as this rational_nt.

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 rational_nt.
falseotherwise.

Definition at line 1553 of file expansion_nt.h.

◆ num() [1/2]

expansion_nt & GEO::rational_nt::num ( )
inline

gets the numerator.

Returns
a reference to the numerator.

Definition at line 1162 of file expansion_nt.h.

◆ num() [2/2]

const expansion_nt & GEO::rational_nt::num ( ) const
inline

gets the numerator.

Returns
a const reference to the numerator.

Definition at line 1146 of file expansion_nt.h.

◆ operator*() [1/2]

rational_nt GEO::rational_nt::operator* ( const rational_nt rhs) const
inline

Computes the product between two rational_nts.

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

Definition at line 1329 of file expansion_nt.h.

◆ operator*() [2/2]

rational_nt GEO::rational_nt::operator* ( double  rhs) const
inline

Computes the product between a rational_nt and a double.

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

Definition at line 1379 of file expansion_nt.h.

◆ operator*=() [1/2]

rational_nt & GEO::rational_nt::operator*= ( const rational_nt rhs)
inline

Multiplies this rational_nt by a rational_nt.

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

Definition at line 1221 of file expansion_nt.h.

◆ operator*=() [2/2]

rational_nt & GEO::rational_nt::operator*= ( double  rhs)
inline

Multiplies this rational_nt 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 rational_nt with
Returns
the new value of this rational_nt

Definition at line 1266 of file expansion_nt.h.

◆ operator+() [1/2]

rational_nt GEO::rational_nt::operator+ ( const rational_nt rhs) const
inline

Computes the sum of two rational_nts.

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

Definition at line 1291 of file expansion_nt.h.

◆ operator+() [2/2]

rational_nt GEO::rational_nt::operator+ ( double  rhs) const
inline

Computes the sum of a rational_nt and a double.

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

Definition at line 1355 of file expansion_nt.h.

◆ operator+=() [1/2]

rational_nt & GEO::rational_nt::operator+= ( const rational_nt rhs)
inline

Adds a rational_nt to this rational_nt.

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

Definition at line 1191 of file expansion_nt.h.

◆ operator+=() [2/2]

rational_nt & GEO::rational_nt::operator+= ( double  rhs)
inline

Adds a double to this rational_nt.

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

Definition at line 1243 of file expansion_nt.h.

◆ operator-() [1/3]

rational_nt GEO::rational_nt::operator- ( ) const
inline

Computes the opposite of this rational_nt.

Returns
the opposite of this rational_nt

Definition at line 1404 of file expansion_nt.h.

◆ operator-() [2/3]

rational_nt GEO::rational_nt::operator- ( const rational_nt rhs) const
inline

Computes the difference between two rational_nts.

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

Definition at line 1310 of file expansion_nt.h.

◆ operator-() [3/3]

rational_nt GEO::rational_nt::operator- ( double  rhs) const
inline

Computes the difference between a rational_nt and a double.

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

Definition at line 1367 of file expansion_nt.h.

◆ operator-=() [1/2]

rational_nt & GEO::rational_nt::operator-= ( const rational_nt rhs)
inline

Subtracts a rational_nt to this rational_nt.

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

Definition at line 1206 of file expansion_nt.h.

◆ operator-=() [2/2]

rational_nt & GEO::rational_nt::operator-= ( double  rhs)
inline

Subtracts a double from this rational_nt.

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

Definition at line 1253 of file expansion_nt.h.

◆ operator/() [1/2]

rational_nt GEO::rational_nt::operator/ ( const rational_nt rhs) const
inline

Computes the ratio between two rational_nts.

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

Definition at line 1342 of file expansion_nt.h.

◆ operator/() [2/2]

rational_nt GEO::rational_nt::operator/ ( double  rhs) const
inline

Computes the ratio between a rational_nt and a double.

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

Definition at line 1391 of file expansion_nt.h.

◆ operator/=() [1/2]

rational_nt & GEO::rational_nt::operator/= ( const rational_nt rhs)
inline

Divides this rational_nt by a rational_nt.

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

Definition at line 1232 of file expansion_nt.h.

◆ operator/=() [2/2]

rational_nt & GEO::rational_nt::operator/= ( double  rhs)
inline

Divides this rational_nt 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 rational_nt with
Returns
the new value of this rational_nt

Definition at line 1279 of file expansion_nt.h.

◆ operator<() [1/2]

bool GEO::rational_nt::operator< ( const rational_nt rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1454 of file expansion_nt.h.

◆ operator<() [2/2]

bool GEO::rational_nt::operator< ( double  rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1498 of file expansion_nt.h.

◆ operator<=() [1/2]

bool GEO::rational_nt::operator<= ( const rational_nt rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1465 of file expansion_nt.h.

◆ operator<=() [2/2]

bool GEO::rational_nt::operator<= ( double  rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1509 of file expansion_nt.h.

◆ operator=() [1/2]

rational_nt & GEO::rational_nt::operator= ( const rational_nt rhs)
inline

Assignment operator.

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

Definition at line 1125 of file expansion_nt.h.

◆ operator=() [2/2]

rational_nt & GEO::rational_nt::operator= ( rational_nt &&  rhs)
inline

Assignment operator with move semantics.

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

Definition at line 1136 of file expansion_nt.h.

◆ operator>() [1/2]

bool GEO::rational_nt::operator> ( const rational_nt rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1432 of file expansion_nt.h.

◆ operator>() [2/2]

bool GEO::rational_nt::operator> ( double  rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1476 of file expansion_nt.h.

◆ operator>=() [1/2]

bool GEO::rational_nt::operator>= ( const rational_nt rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1443 of file expansion_nt.h.

◆ operator>=() [2/2]

bool GEO::rational_nt::operator>= ( double  rhs) const
inline

Compares this rational_nt with another one.

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

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

Definition at line 1487 of file expansion_nt.h.

◆ optimize()

void GEO::rational_nt::optimize ( )
inline

Optimizes the internal representation without changing the represented value.

this function can reduce the length of an expansion

Definition at line 1179 of file expansion_nt.h.

◆ sign()

Sign GEO::rational_nt::sign ( ) const
inline

Gets the sign of this rational_nt.

Returns
the sign of this rational_nt, computed exactly.

Definition at line 1528 of file expansion_nt.h.

Friends And Related Symbol Documentation

◆ operator!=() [1/3]

bool operator!= ( const rational_nt a,
const rational_nt b 
)
related

Tests whether two rational_nts 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 1657 of file expansion_nt.h.

◆ operator!=() [2/3]

bool operator!= ( const rational_nt a,
double  b 
)
related

Tests whether a rational_nt 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 1669 of file expansion_nt.h.

◆ operator!=() [3/3]

bool operator!= ( double  a,
const rational_nt b 
)
related

Tests whether a double differs from a rational_nt.

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 1681 of file expansion_nt.h.

◆ operator*()

rational_nt operator* ( double  a,
const rational_nt b 
)
related

Computes the product of a double and a rational_nt.

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

Definition at line 1595 of file expansion_nt.h.

◆ operator+()

rational_nt operator+ ( double  a,
const rational_nt b 
)
related

Computes the sum of a double and a rational_nt.

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

Definition at line 1571 of file expansion_nt.h.

◆ operator-()

rational_nt operator- ( double  a,
const rational_nt b 
)
related

Computes the difference between a double and a rational_nt.

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

Definition at line 1582 of file expansion_nt.h.

◆ operator/()

rational_nt operator/ ( double  a,
const rational_nt b 
)
related

Computes the ratio between a double and a rational_nt.

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

Definition at line 1606 of file expansion_nt.h.

◆ operator==() [1/3]

bool operator== ( const rational_nt a,
const rational_nt b 
)
related

Tests equality between two rational_nts.

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 1621 of file expansion_nt.h.

◆ operator==() [2/3]

bool operator== ( const rational_nt a,
double  b 
)
related

Tests equality between a rational_nt 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 1633 of file expansion_nt.h.

◆ operator==() [3/3]

bool operator== ( double  a,
const rational_nt b 
)
related

Tests equality between a double and a rational_nt.

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 1645 of file expansion_nt.h.


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