Learning objectives
The class rational 
Rational numbers are used excessively in geometry. Actually,
they are used wherever exact computation is needed and no higher algebraic operations such as extracting exact square roots are involved. LEDA
accommodates this need by providing the class rational
.
An object of type rational
represents
a rational number, that is, it has an (integer) numerator and an
(integer) denominator. Numerator and denominator are of arbitrary length. Just as the class
integer
, this class provides all usual
arithmetic operators such as +
,

, *
,
/
, and %
, so that
rational
s can be used in all usual arithmetic
C++ expressions.
As a (nongeometrical) application of
rational
, we compute the value of a
continued fraction. A continued fraction is
a representation of a real number in the form
where all a_{i} are nonnegative integer numbers. Each continued fraction is a representation of a (positive) real number and each (positive) real number has a continued fraction representation. These representations are very common in number theory.
For example, the continued fraction
where all a_{i} are equal to 1 is the representation of the number Φ = (1+√5)/2 = 1.61803...^{[44]} , the Golden Ratio, a number that is very important in the art world, where it has been considered since ancient times to be the most pleasing ratio for many kinds of design ([7]). Furthermore, Φ frequently occurs in number theory; for example, the ratio F_{n+1}/F_{n} of two consecutive Fibonacci numbers is known to converge to Φ.
The following program uses the above continued fraction representation to approximate Φ.
#include <LEDA/rational.h> using leda::rational; using std::cout; using std::endl; int main() { rational r(1,1); while(1) { cout << r << " is approx. " << r.to_float() << endl; r = 1 + 1 / r; } }
The program outputs
1/1 is approx. 1 2/1 is approx. 2 3/2 is approx. 1.5 5/3 is approx. 1.66667 8/5 is approx. 1.6 13/8 is approx. 1.625 21/13 is approx. 1.61538 34/21 is approx. 1.61905 55/34 is approx. 1.61765 89/55 is approx. 1.61818 144/89 is approx. 1.61798 233/144 is approx. 1.61806 377/233 is approx. 1.61803 610/377 is approx. 1.61804 987/610 is approx. 1.61803 1597/987 is approx. 1.61803 ...
As we can see, this approximation converges quite fast to Φ. Furthermore, the numerators (and denominators) exactly constitute the sequence of Fibonacci numbers.
The program shows how easy it is to use the class
rational
. A rational
is created by specifying its numerator and denominator in the
constructor:
rational r( integer n, integer d );
A rational
can be created also from a
double
:
rational r( double d );
This initializes r
to have the same value as
d
, where “value” means the quotient of the numerator and the denominator.
Numerator and denominator of a
rational
can be accessed via
integer r.numerator(); integer r.denominator();
In long computations, the numerators and denominators of rational numbers tend to become quite huge, so sometimes it may be worthwhile to normalize intermediate results. This is achieved by
rational& r.normalize();
which returns a (reference to a) normalized rational
, that is, whose numerator and denominator are relatively prime.
Normalization is done internally by calling Euclid's
algorithm to find a common factor. If, however, a common factor f
is known a priori, a rational
can also be
simplified by
rational& r.simplify( const integer& f );
which returns (a reference to) the simplified rational
.
Important  

LEDA's rational numbers are not necessarily normalized,
that is, the numerator and denominator of a

To yield a floating point approximation of a
rational
, the method
double r.to_float();
can be used.
The class rational
has some other
useful methods, for example, for negating and inverting
rationals, for turning them into strings, for computing roots
and powers of rationals, and for rounding them to the nearest
integer.
A description of all these methods and more information on
the class rational
can be found on the
corresponding manual
page.
LEDA's rational
s are about 30100
times slower than double
, but much faster than
algebraic real
numbers. They should be used if exact arithmetic for
rational numbers is needed and no exact higher algebraic
operations are involved.
^{[44] }That is easy to see: Just solve the quadratic equation Φ = 1 + 1 / Φ.