Regina Calculation Engine
|
Represents an arbitrary precision rational number. More...
#include <maths/rational.h>
Public Member Functions | |
Rational () | |
Initialises to 0/1. More... | |
Rational (const Rational &value) | |
Initialises to the given rational value. More... | |
template<bool supportInfinity> | |
Rational (const IntegerBase< supportInfinity > &value) | |
Initialises to the given integer value. More... | |
Rational (long value) | |
Initialises to the given integer value. More... | |
template<bool supportInfinity> | |
Rational (const IntegerBase< supportInfinity > &newNum, const IntegerBase< supportInfinity > &newDen) | |
Initialises to newNum/newDen. More... | |
Rational (long newNum, unsigned long newDen) | |
Initialises to newNum/newDen. More... | |
~Rational () | |
Destroys this rational. More... | |
Rational & | operator= (const Rational &value) |
Sets this rational to the given rational value. More... | |
template<bool supportInfinity> | |
Rational & | operator= (const IntegerBase< supportInfinity > &value) |
Sets this rational to the given integer value. More... | |
Rational & | operator= (long value) |
Sets this rational to the given integer value. More... | |
void | swap (Rational &other) |
Swaps the values of this and the given rational. More... | |
Integer | numerator () const |
Returns the numerator of this rational. More... | |
Integer | denominator () const |
Returns the denominator of this rational. More... | |
Rational | operator* (const Rational &r) const |
Calculates the product of two rationals. More... | |
Rational | operator/ (const Rational &r) const |
Calculates the ratio of two rationals. More... | |
Rational | operator+ (const Rational &r) const |
Calculates the sum of two rationals. More... | |
Rational | operator- (const Rational &r) const |
Calculates the difference of two rationals. More... | |
Rational | operator- () const |
Determines the negative of this rational. More... | |
Rational | inverse () const |
Calculates the inverse of this rational. More... | |
Rational | abs () const |
Determines the absolute value of this rational. More... | |
Rational & | operator+= (const Rational &other) |
Adds the given rational to this. More... | |
Rational & | operator-= (const Rational &other) |
Subtracts the given rational from this. More... | |
Rational & | operator*= (const Rational &other) |
Multiplies the given rational by this. More... | |
Rational & | operator/= (const Rational &other) |
Divides this by the given rational. More... | |
void | negate () |
Negates this rational. More... | |
void | invert () |
Inverts this rational. More... | |
bool | operator== (const Rational &compare) const |
Determines if this is equal to the given rational. More... | |
bool | operator!= (const Rational &compare) const |
Determines if this is not equal to the given rational. More... | |
bool | operator< (const Rational &compare) const |
Determines if this is less than the given rational. More... | |
bool | operator> (const Rational &compare) const |
Determines if this is greater than the given rational. More... | |
bool | operator<= (const Rational &compare) const |
Determines if this is less than or equal to the given rational. More... | |
bool | operator>= (const Rational &compare) const |
Determines if this is greater than or equal to the given rational. More... | |
double | doubleApprox (bool *inRange=0) const |
Attempts to convert this rational to a real number. More... | |
std::string | TeX () const |
Returns this rational as written using TeX formatting. More... | |
std::ostream & | writeTeX (std::ostream &out) const |
Writes this rational in TeX format to the given output stream. More... | |
Static Public Attributes | |
static const Rational | zero |
Globally available zero. More... | |
static const Rational | one |
Globally available one. More... | |
static const Rational | infinity |
Globally available infinity. More... | |
static const Rational | undefined |
Globally available undefined. More... | |
Friends | |
std::ostream & | operator<< (std::ostream &out, const Rational &rat) |
Writes the given rational to the given output stream. More... | |
Represents an arbitrary precision rational number.
Calculations with Rational objects will be exact.
Infinity (1/0) and undefined (0/0) are catered for. (-1/0) is considered the same as (1/0), and is represented as (1/0). Any operation involving (0/0) will return (0/0).
Since infinity is the same as negative infinity, both infinity plus infinity and infinity minus infinity will return infinity. Infinity divided by infinity returns undefined, as does infinity times zero.
For the purposes of ordering, undefined is the smallest rational and infinity is the largest. Undefined is always equal to itself, and infinity is always equal to itself.
When performing computations on rationals, the results will always be stored in lowest terms (i.e., with relatively prime numerator and denominator), and with a non-negative denominator. However, when constructing a rational number from scratch (e.g., by supplying the numerator and denominator separately), it is your responsibility to ensure that the rational is in lowest terms.