Learn R Programming

numbers (version 0.8-5)

contfrac: Continued Fractions

Description

Evaluate a continued fraction or generate one.

Usage

contfrac(x, tol = 1e-12)

Value

If x is a scalar, it will return a list with components cf

the continued fraction as a vector, rat the rational approximation, and prec the difference between the value and this approximation.

If x is a vector, the continued fraction, then it will return a list with components f the numerical value, p and q the convergents, and prec an estimated precision.

Arguments

x

a numeric scalar or vector.

tol

tolerance; default 1e-12.

Details

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

The continued fraction \([b_0; b_1, \ldots, b_{n-1}]\) is assumed to be finite and neither periodic nor infinite. For implementation uses the representation of continued fractions through 2-by-2 matrices (i.e. Wallis' recursion formula from 1644).

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
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