- x is a number to approximate.
- n is a integer (representing a format).

- rationalapprox(x,n) returns a constant function of the form a/b where a and b are integers. The value a/b is an approximation of x. The quality of this approximation is determined by the parameter n that indicates the number of correct bits that a/b should have.
- The command is not safe in the sense that it is not ensured that the error between a/b and x is less than 2^(-n).
- The following algorithm is used: x is first rounded downwards and upwards to a format of n bits, thus obtaining an interval [xl,xu]. This interval is then developped into a continued fraction as far as the representation is the same for every elements of [xl,xu]. The corresponding fraction is returned.
- Since rational numbers are not a primitive object of Sollya, the fraction is returned as a constant function. This can be quite amazing, because Sollya immediately simplifies a constant function by evaluating it when the constant has to be displayed. To avoid this, you can use print (that displays the expression representing the constant and not the constant itself) or the commands numerator and denominator.

> pi50 = rationalapprox(Pi,50);

> pi100 = rationalapprox(Pi,100);

> print( pi10, ": ", simplify(floor(-log2(abs(pi10-Pi)/Pi))), "bits." );

3.140625 : 11 bits.

> print( pi50, ": ", simplify(floor(-log2(abs(pi50-Pi)/Pi))), "bits." );

85563208 / 27235615 : 51 bits.

> print( pi100, ": ", simplify(floor(-log2(abs(pi100-Pi)/Pi))), "bits." );

4422001152019829 / 1407566683404023 : 100 bits.

> b=rationalapprox(a,4);

> numerator(b); denominator(b);

1

10

> print(simplify(floor(-log2(abs((b-a)/a)))), "bits.");

166 bits.