## `numlib::contfrac` -- the domain of continued fractions

#### Introduction

`numlib::contfrac``(a, n)` creates a continued fraction approximation for `a`, using the first `n` digits of its floating-point evaluation.

#### Creating Elements

`numlib::contfrac(a <,n>)`

#### Parameters

`n` | - | positive integer
`a` | - | numerical expression

#### Details

- If `n` is not given, the value of the variable `DIGITS` is used.

#### Method approx: rational approximation

- `approx(dom cf, positive integer n)`
- - This method returns two rational numbers a and b such that the interval of all rational numbers whose first `n` coefficients coincide with those of `cf` equals [a,b).

#### Method unapprox: find simple continued fraction in interval

- `unapprox(numerical expression a, numerical expression b)`
- - This method returns a rational number in the interval [a,b], represented as a finite continued fraction, such that the number of coefficients of the continued fraction is minimal; among several such continued fractions (which must agree in all coefficients except for the last), that one with minimal last coefficient is chosen.

#### Method _plus: sum of two continued fractions

- `_plus(dom a, dom b)`
- - This method converts `a` and `b` into rationals, adds them, and returns the sum converted back into a continued fraction.
  - It overloads the function `_plus` of the system kernel.

#### Method _mult: product of two continued fractions

- `_mult(dom a, dom b)`
- - This method converts `a` and `b` into rationals, multiplies them, and returns the product converted back into a continued fraction.
  - It overloads the function `_mult` of the system kernel.

#### Method _invert: Inverse of a continued fraction

- `_invert(dom a)`
- - This method computes 1/a.
  - Inverting a continued fraction means a shift of the coefficients by one to the left or to the right.
  - This method overloads the function `_invert` of the system kernel.

#### Method _power: Integer power of a continued fraction

- `_power(dom a, integer n)`
- - This method multiplies `a` by itself `n` times, or, if `n` is negative, the inverse of `a` by itself `-n` times.

#### Example 1

`numlib::contfrac` can also compute continued fraction expansions of irrational numbers:

    >> a:= numlib::contfrac(PI, 5):
       b:= numlib::contfrac(sqrt(7), 2):
       a, b

                          1                          1
                ---------------------- + 3, ------------------- + 2
                        1                          1
                ------------------ + 7      --------------- + 1
                      1                          1
                ------------- + 15          ----------- + 1
                    1                          1
                --------- + 1               ------- + 1
                 1                           1
                --- + 292                   --- + 4
                ...                         ...

All basic arithmetical operations are available:

    >> a + b, a*b, a^3

                      1                1               1
               --------------- + 5, ------- + 8, ------------- + 31
                    1                1              1
               ----------- + 1      --- + 3      ------- + 159
                  1                 ...           1
               ------- + 3                       --- + 3
                1                                ...
               --- + 1
               ...

#### Changes

- used to be called `contfrac`

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000