Definition
An instance q of type rational is a rational number where the numerator and the denominator are both of type integer.
#include < LEDA/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 its 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.
integer | q.numerator() | returns the numerator of q. |
integer | q.denominator() | returns the denominator of q. |
rational& | q.simplify(integer a) | simplifies q by a.
Precondition a divides the numerator and the denominator of q. |
rational& | q.normalize() | normalizes q. |
void | q.negate() | negates q. |
void | q.invert() | inverts q. |
rational | q.inverse() | returns the inverse of q. |
double | to_double() | returns a double floating point approximation of q. |
string | q.to_string() | returns a string representation of q. |
Non-member functions
int | sign(rational q) | returns the sign of q. |
rational | abs(rational q) | returns the absolute value of q. |
rational | sqr(rational q) | returns the square of q. |
integer | trunc(rational q) | returns the integer with the next smaller absolute value. |
rational | pow(rational q, int n) | returns the n-th power of q. |
rational | pow(rational q, integer a) | |
returns the a-th power of q. | ||
integer | floor(rational q) | returns the next smaller integer. |
integer | ceil(rational q) | returns the next bigger integer. |
integer | round(rational q) | rounds q to the nearest integer. |
double | Double(rational q) | returns a double floating point approximation of q. |
rational | small_rational_between(rational p, rational q) | |
returns a rational number between p and q whose denominator is as small as possible. | ||
rational | small_rational_near(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.