numbers (version 0.7-5)

contFrac: Continous Fractions

Description

Evaluate a continuous fraction or generate one.

Usage

contFrac(x, tol = 1e-06)

Arguments

x

a numeric scalar or vector.

tol

tolerance; default 1e-6 to make a nicer appearance for pi.

Value

Either a numeric value, or a list with components cf, numeric vector representing the continuous fraction \([b_0; b_1, \ldots, b_{n-1}]\); rat, the rational number as a vector with (numerator, denumerator); and prec, the difference between x and the value of the contimuous fraction.

Details

If x is a scalar its continuous fraction will be generated up to the accuracy prescribed in tol. If it is of length greater 1, the function assumes this is a continuous fraction and computes its value.

For implementation contfrac uses the representation of continuous fractions through 2-by-2 matrices, i.e. the recursion formula.

References

Hardy, G. H., and E. M. Wright (1979). An Introduction to the Theory of Numbers. Fifth Edition, Oxford University Press, New York.

See Also

cf2num, ratFarey

Examples

Run this code
# NOT RUN {
contFrac(pi)
contFrac(c(3, 7, 15, 1))        # rational Approx: 355/113

contFrac(0.555)                 #  0  1  1  4 22
contFrac(c(1, rep(2, 25)))      #  1.414213562373095, sqrt(2)
# }

Run the code above in your browser using DataLab