ratFarey: Farey Approximation and Series

View source: R/ratFarey.R

Farey NumbersR Documentation

Farey Approximation and Series

Description

Rational approximation of real numbers through Farey fractions.

Usage

ratFarey(x, n, upper = TRUE)

farey_seq(n)

Arguments

x

real number.

n

integer, highest allowed denominator in a rational approximation.

upper

logical; shall the Farey fraction be grater than x.

Details

Rational approximation of real numbers through Farey fractions, i.e. find for x the nearest fraction in the Farey series of rational numbers with denominator not larger than n.

farey_seq(n) generates the full Farey sequence of rational numbers with denominators not larger than n. Returns the fractions as 'big rational' class in 'gmp'.

Value

Returns a vector with two natural numbers, nominator and denominator.

Note

farey_seq is very slow even for n > 40, due to the handling of rational numbers as 'big rationals'.

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

contFrac

Examples

ratFarey(pi, 100)                          # 22/7    0.0013
ratFarey(pi, 100, upper = FALSE)           # 311/99  0.0002
ratFarey(-pi, 100)                         # -22/7
ratFarey(pi - 3, 70)                       # pi ~= 3 + (3/8)^2
ratFarey(pi, 1000)                         # 355/113
ratFarey(pi, 10000, upper = FALSE)         # id.
ratFarey(pi, 1e5, upper = FALSE)           # 312689/99532 - pi ~= 3e-11

ratFarey(4/5, 5)                           # 4/5
ratFarey(4/5, 4)                           # 1/1
ratFarey(4/5, 4, upper = FALSE)            # 3/4

numbers documentation built on Nov. 23, 2022, 9:06 a.m.