Definition
An instance q of type rational is a rational number where the numerator and the denominator are both of type integer.
#include < LEDA/numbers/rational.h >
Creation
rational | q | creates an instance q of type rational. |
rational | q(integer n) | creates an instance q of type rational and initializes it with the integer n. |
rational | q(integer n, integer d) | creates an instance q of type rational and initializes it to the rational number n/d. |
rational | q(double x) | creates an instance q of type rational and initializes it with the value of x. |
Operations
The arithmetic operations +, -, *, /, + =, - =, * =, / =, -(unary), + +, - -, the comparison operations <, < =, >, > =, = =, ! = and the stream operations are all available.
void | q.negate() | negates q. |
void | q.invert() | inverts q. |
rational | q.inverse() | returns the inverse of q. |
integer | q.numerator() | returns the numerator of q. |
integer | q.denominator() | returns the denominator of q. |
rational& | q.simplify(const integer& a) | |
simplifies q by a.
Precondition a divides the numerator and the denominator of q. |
||
rational& | q.normalize() | normalizes q. |
double | to_float() | returns a double floating point approximation of q. If the q is approximable by a normalized, finite floating point number, the error is 3ulps, i.e., three units in the last place. |
string | q.to_string() | returns a string representation of q. |
Non-member functions
int | sign(const rational& q) | returns the sign of q. |
rational | abs(const rational& q) | returns the absolute value of q. |
rational | sqr(const rational& q) | returns the square of q. |
integer | trunc(const rational& q) | returns the integer with the next smaller absolute value. |
rational | pow(const rational& q, int n) | |
returns the n-th power of q. | ||
rational | pow(const rational& q, integer a) | |
returns the a-th power of q. | ||
integer | floor(const rational& q) | returns the next smaller integer. |
integer | ceil(const rational& q) | returns the next bigger integer. |
integer | round(const rational& q) | rounds q to the nearest integer. |
rational | small_rational_between(const rational& p, const rational& q) | |
returns a rational number between p and q whose denominator is as small as possible. | ||
rational | small_rational_near(const rational& p, rational eps) | |
returns a rational number between p - eps and p + eps whose denominator is as small as possible. |
Implementation
A rational is implemented by two integer numbers which represent the numerator and the denominator. The sign is represented by the sign of the numerator.