Public Member Functions | |
Standard constructors. | |
Constructor from numerator and denominator. | |
RationalNumber (int num, unsigned int denom) | |
Default constructor. Initialize to zero. | |
RationalNumber () | |
Default constructor. Initialize to zero. | |
RationalNumber (const RationalNumber &nb) | |
Copy constructor. | |
template<typename T > | |
RationalNumber (const T num) | |
Generic constructor. | |
Accessors | |
Get the numerator. | |
const int & | num () const |
Get the denominator. | |
const unsigned & | denom () const |
Get the denominator. | |
Usual Operators. | |
Usual numerical operator. | |
RationalNumber | operator+ (const RationalNumber &nb) const |
RationalNumber | operator- (const RationalNumber &nb) const |
RationalNumber | operator- () const |
RationalNumber | operator* (const RationalNumber &nb) const |
RationalNumber | operator/ (const RationalNumber &nb) const |
RationalNumber & | operator+= (const RationalNumber &nb) |
RationalNumber & | operator-= (const RationalNumber &nb) |
RationalNumber & | operator*= (const RationalNumber &nb) |
RationalNumber & | operator/= (const RationalNumber &nb) |
bool | operator< (const RationalNumber &nb) const |
bool | operator> (const RationalNumber &nb) const |
bool | operator<= (const RationalNumber &nb) const |
bool | operator>= (const RationalNumber &nb) const |
bool | operator== (const RationalNumber &nb) const |
bool | operator!= (const RationalNumber &nb) const |
Type conversion methods. | |
int | to_int () const |
Provide Explicit cast operator. | |
double | to_double () const |
Provide Explicit cast operator. | |
Protected Member Functions | |
Fraction simplification | |
RationalNumber & | set_result () |
Simplifies the fraction. | |
RationalNumber & | set_result (int num, unsigned int denom) |
Simplifies the fraction. |
This is a rational numbers implementation.
The way the constructor works ables us to work only on simplified fractions. So, the numerator (num_) and denominator (denom_) are always relatively prime.
Even after operations, the obtained fraction is simplified.
Definition at line 59 of file rational_number.hh.
RationalNumber | ( | const T | num | ) | [inline, explicit] |
Generic constructor.
Definition at line 61 of file rational_number.hxx.
int to_int | ( | ) | const [inline] |
Provide Explicit cast operator.
to_int() and to_double() allow us to get respectively an integer and a double from the rational number. These numbers are obtained by dividing the fraction's numerator and denominator.
Definition at line 230 of file rational_number.hxx.
double to_double | ( | ) | const [inline] |
Provide Explicit cast operator.
to_int() and to_double() allow us to get respectively an integer and a double from the rational number. These numbers are obtained by dividing the fraction's numerator and denominator.
Definition at line 237 of file rational_number.hxx.
RationalNumber & set_result | ( | ) | [inline, protected] |
Simplifies the fraction.
set_result() is used to simplify the fraction. We use the GCD (Greatest Common Divisor) algorithm. When there are no arguments, it simply checks the numerator and denominator and modifies them if needed.
Definition at line 88 of file rational_number.hxx.
Referenced by RationalNumber::RationalNumber().
RationalNumber & set_result | ( | int | num, | |
unsigned int | denom | |||
) | [inline, protected] |
Simplifies the fraction.
set_result() is used to simplify the fraction. We use the GCD (Greatest Common Divisor) algorithm. When there are no arguments, it simply checks the numerator and denominator and modifies them if needed.
Definition at line 79 of file rational_number.hxx.