frac {cwhmisc} | R Documentation |
Split off fractional part of a number, compute and evaluate continuous fractions.
contfrac( x, depth = 13, f=floor ) evalcfr( cf ) toCFrac( x, depth=5) toCFrac2( x, depth=5)
x |
Real |
f |
function to use, normally 'floor', otherwise 'round' or 'trunc' |
cf |
Vector of integers representing the continued fraction of a real number |
depth |
Integer |
int
integer part truncate towards 0.
frac
fractional part, if d
is missing; else
round(10^d*fractional part), i.e. the fractional part as "integer" (rounded).
contfrac
Convert to simple continued fraction representation, cf := a_1 + 1/(a_2 + 1/(a_3 ... )).
evalcfr
Evaluate simple continued fraction to corresponding real.
toCFrac
Build rational approximation num/den
to x
using forward continued fraction recursion to a depth of depth
. Stopping criterion: either depth
is reached, or abs(x - num/den) is increasing again.
toCFrac2
same as toCFrac
, but vectors of partial numerators and denominators are returned.
d
not missing is practical for use in dc
For confrac
see also link[MASS]{fractions}
.
from Mathematics 5335 Fall 2009
The Euclidean Algorithm and Continued Fractions
Christian W. Hoffmann <christian@echoffmann.ch>
(pcf <- contfrac(pi)) # 3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, (1) ## last integer incorrect due to rounding errors evalcfr(pcf)-pi # 0 ## To see the first approximants of pi, all of them famous: for(ii in 1:15) {x<-toCFrac(pi,ii) print(paste(ii,":",x$num,"/",x$den,"=")) print(paste(formatFix(x$num/x$den,15),", error = ",x$num/x$den-pi))} # Note how the approximations taper off after depth 5: # 10 3959189 / 1260249 = 3.141592653515298 -7.44955208631382e-11" ## Same, all at once: F <- toCFrac2(pi,5) # $num 3 22 333 355 $den 1 7 106 113 toCFrac( pi, 10 ) #