#include <nrational.h>
Public Member Functions | |
NRational () | |
Initialises to 0/1. | |
NRational (const NRational &value) | |
Initialises to the given rational value. | |
NRational (const NLargeInteger &value) | |
Initialises to the given integer value. | |
NRational (long value) | |
Initialises to the given integer value. | |
NRational (const NLargeInteger &newNum, const NLargeInteger &newDen) | |
Initialises to newNum/newDen. | |
NRational (long newNum, unsigned long newDen) | |
Initialises to newNum/newDen. | |
virtual | ~NRational () |
Destroys this rational. | |
NRational & | operator= (const NRational &value) |
Sets this rational to the given rational value. | |
NRational & | operator= (const NLargeInteger &value) |
Sets this rational to the given integer value. | |
NRational & | operator= (long value) |
Sets this rational to the given integer value. | |
NLargeInteger | getNumerator () const |
Returns the numerator of this rational. | |
NLargeInteger | getDenominator () const |
Returns the denominator of this rational. | |
NRational | operator * (const NRational &r) const |
Calculates the product of two rationals. | |
NRational | operator/ (const NRational &r) const |
Calculates the ratio of two rationals. | |
NRational | operator+ (const NRational &r) const |
Calculates the sum of two rationals. | |
NRational | operator- (const NRational &r) const |
Calculates the difference of two rationals. | |
NRational | operator- () const |
Determines the negative of this rational. | |
NRational | inverse () const |
Calculates the inverse of this rational. | |
NRational & | operator+= (const NRational &other) |
Adds the given rational to this. | |
NRational & | operator-= (const NRational &other) |
Subtracts the given rational from this. | |
NRational & | operator *= (const NRational &other) |
Multiplies the given rational by this. | |
NRational & | operator/= (const NRational &other) |
Divides this by the given rational. | |
void | negate () |
Negates this rational. | |
void | invert () |
Inverts this rational. | |
bool | operator== (const NRational &compare) const |
Determines if this is equal to the given rational. | |
bool | operator!= (const NRational &compare) const |
Determines if this is not equal to the given rational. | |
bool | operator< (const NRational &compare) const |
Determines if this is less than the given rational. | |
bool | operator> (const NRational &compare) const |
Determines if this is greater than the given rational. | |
bool | operator<= (const NRational &compare) const |
Determines if this is less than or equal to the given rational. | |
bool | operator>= (const NRational &compare) const |
Determines if this is greater than or equal to the given rational. | |
Static Public Attributes | |
const NRational | zero |
Globally available zero. | |
const NRational | one |
Globally available one. | |
const NRational | infinity |
Globally available infinity. | |
const NRational | undefined |
Globally available undefined. | |
Friends | |
std::ostream & | operator<< (std::ostream &out, const NRational &rat) |
Writes the given rational to the given output stream. |
Calculations with NRational 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.
Rationals will always be stored in lowest terms with non-negative denominator.
|
Initialises to 0/1.
|
|
Initialises to the given rational value.
|
|
Initialises to the given integer value.
|
|
Initialises to the given integer value.
|
|
Initialises to newNum/newDen.
|
|
Initialises to newNum/newDen.
|
|
Destroys this rational.
|
|
Returns the denominator of this rational. Note that rationals are always stored in lowest terms with non-negative denominator.
|
|
Returns the numerator of this rational. Note that rationals are always stored in lowest terms with non-negative denominator. Infinity will be stored as 1/0.
|
|
Calculates the inverse of this rational. This rational is not changed.
|
|
Inverts this rational. This rational is changed to reflect the result. |
|
Negates this rational. This rational is changed to reflect the result. |
|
Calculates the product of two rationals. This rational is not changed.
|
|
Multiplies the given rational by this. This rational is changed to reflect the result.
|
|
Determines if this is not equal to the given rational.
|
|
Calculates the sum of two rationals. This rational is not changed.
|
|
Adds the given rational to this. This rational is changed to reflect the result.
|
|
Determines the negative of this rational. This rational is not changed.
|
|
Calculates the difference of two rationals. This rational is not changed.
|
|
Subtracts the given rational from this. This rational is changed to reflect the result.
|
|
Calculates the ratio of two rationals. This rational is not changed.
|
|
Divides this by the given rational. This rational is changed to reflect the result.
|
|
Determines if this is less than the given rational.
|
|
Determines if this is less than or equal to the given rational.
|
|
Sets this rational to the given integer value.
|
|
Sets this rational to the given integer value.
|
|
Sets this rational to the given rational value.
|
|
Determines if this is equal to the given rational.
|
|
Determines if this is greater than the given rational.
|
|
Determines if this is greater than or equal to the given rational.
|
|
Writes the given rational to the given output stream.
Infinity will be written as
|
|
Globally available infinity. Note that both 1/0 and -1/0 evaluate to this same rational. When queried, the representation 1/0 will be returned. |
|
Globally available one.
|
|
Globally available undefined. This is represented as 0/0. |
|
Globally available zero.
|