ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::Rational Class Reference

a fraction in lowest terms, the denominator positive More...

#include <ADS/numeric/Rational.h>

Public member functions

 Rational (BigInteger num=0, BigInteger den=1)
 the fraction \( \mathit{num} / \mathit{den} \), brought to lowest terms with a positive denominator by one gcd
const BigInteger & num (void) const
 the numerator \( u \)
const BigInteger & den (void) const
 the denominator \( u' > 0 \)
std::string toString (void) const
 the text u/u', or u when \( u' = 1 \)
Rational operator- (void) const
 the opposite \( - u / u' \)

Friends

Rational operator+ (const Rational &a, const Rational &b)
 the sum \( a + b \), by the gcds of the smaller numbers
Rational operator- (const Rational &a, const Rational &b)
 the difference \( a - b \), as \( a + ( - b ) \)
Rational operator* (const Rational &a, const Rational &b)
 the product \( a b \), by the gcds that cancel crosswise
Rational operator/ (const Rational &a, const Rational &b)
 the quotient \( a / b \), as \( a \) times the inverse of \( b \)
bool operator== (const Rational &a, const Rational &b)=default
 true if the two fractions are equal, i.e., their terms are
std::strong_ordering operator<=> (const Rational &a, const Rational &b)
 the order of \( a \) and \( b \), by the signs and then by \( u v' \) against \( v u' \)

Detailed description

a fraction in lowest terms, the denominator positive

Constructor and destructor documentation

◆ Rational()

ADS::Rational::Rational ( BigInteger num = 0,
BigInteger den = 1 )

the fraction \( \mathit{num} / \mathit{den} \), brought to lowest terms with a positive denominator by one gcd

Precondition
\( \mathit{den} \ne 0 \)

Member function documentation

◆ den()

const BigInteger & ADS::Rational::den ( void ) const
inlinenodiscard

the denominator \( u' > 0 \)

◆ num()

const BigInteger & ADS::Rational::num ( void ) const
inlinenodiscard

the numerator \( u \)

◆ operator-()

Rational ADS::Rational::operator- ( void ) const

the opposite \( - u / u' \)

◆ toString()

std::string ADS::Rational::toString ( void ) const
nodiscard

the text u/u', or u when \( u' = 1 \)

◆ operator*

Rational operator* ( const Rational & a,
const Rational & b )
friend

the product \( a b \), by the gcds that cancel crosswise

In pseudocode (section 4.5.1 of Knuth, beyond the book), \( a = u / u' \) and \( b = v / v' \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ a \cdot b \cr 2 & \quad \textbf{if}\ u = 0\ \textbf{or}\ v = 0\ \textbf{then return} \ 0 \cr 3 & \quad d_1 \gets \mathrm{gcd}( u , v' );\ d_2 \gets \mathrm{gcd}( u' , v ) \cr 4 & \quad \textbf{return}\ ( ( u / d_1 ) ( v / d_2 ) ) / ( ( u' / d_2 ) ( v' / d_1 ) ) \end{array}} \]

◆ operator+

Rational operator+ ( const Rational & a,
const Rational & b )
friend

the sum \( a + b \), by the gcds of the smaller numbers

In pseudocode (the method of Henrici, section 4.5.1 of Knuth, beyond the book), \( a = u / u' \) and \( b = v / v' \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ a + b \cr 2 & \quad d_1 \gets \mathrm{gcd}( u' , v' ) \cr 3 & \quad \textbf{if}\ d_1 = 1\ \textbf{then return}\ ( u v' + v u' ) / ( u' v' ) \cr 4 & \quad t \gets u ( v' / d_1 ) + v ( u' / d_1 ) \cr 5 & \quad \textbf{if}\ t = 0\ \textbf{then return}\ 0 \cr 6 & \quad d_2 \gets \mathrm{gcd}( t , d_1 ) \cr 7 & \quad \textbf{return}\ ( t / d_2 ) / ( ( u' / d_1 ) ( v' / d_2 ) ) \end{array}} \]

◆ operator-

Rational operator- ( const Rational & a,
const Rational & b )
friend

the difference \( a - b \), as \( a + ( - b ) \)

◆ operator/

Rational operator/ ( const Rational & a,
const Rational & b )
friend

the quotient \( a / b \), as \( a \) times the inverse of \( b \)

Precondition
\( b \ne 0 \)

◆ operator<=>

std::strong_ordering operator<=> ( const Rational & a,
const Rational & b )
friend

the order of \( a \) and \( b \), by the signs and then by \( u v' \) against \( v u' \)

◆ operator==

bool operator== ( const Rational & a,
const Rational & b )
friend

true if the two fractions are equal, i.e., their terms are


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