|
ADS
Algorithms and Data Structures
|
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' \) | |
a fraction in lowest terms, the denominator positive
| 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
|
inlinenodiscard |
the denominator \( u' > 0 \)
|
inlinenodiscard |
the numerator \( u \)
| Rational ADS::Rational::operator- | ( | void | ) | const |
the opposite \( - u / u' \)
|
nodiscard |
the text u/u', or u when \( u' = 1 \)
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}} \]
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}} \]
the difference \( a - b \), as \( a + ( - b ) \)
the quotient \( a / b \), as \( a \) times the inverse of \( b \)
the order of \( a \) and \( b \), by the signs and then by \( u v' \) against \( v u' \)
true if the two fractions are equal, i.e., their terms are