Description Usage Arguments Value Note Author(s) Examples
Split off fractional part of a number, compute and evaluate continuous fractions.
1 2 3 4 |
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>
1 2 3 4 5 6 7 8 9 10 11 12 | (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 ) #
|
Loading required package: lattice
Loading required package: grid
[1] 3 7 15 1 292 1 1 1 2 1 3 1 14 3
[1] 0
[1] "1 : 3 / 1 ="
[1] " 3.000000000000000 , error = -0.141592653589793"
[1] "2 : 22 / 7 ="
[1] " 3.142857142857143 , error = 0.00126448926734968"
[1] "3 : 333 / 106 ="
[1] " 3.141509433962264 , error = -8.32196275291075e-05"
[1] "4 : 355 / 113 ="
[1] " 3.141592920353982 , error = 2.66764189404967e-07"
[1] "5 : 103993 / 33102 ="
[1] " 3.141592653011902 , error = -5.77890624242627e-10"
[1] "6 : 104348 / 33215 ="
[1] " 3.141592653921421 , error = 3.31628058347633e-10"
[1] "7 : 208341 / 66317 ="
[1] " 3.141592653467437 , error = -1.22356347276309e-10"
[1] "8 : 937712 / 298483 ="
[1] " 3.141592653517956 , error = -7.18372028529757e-11"
[1] "9 : 3021477 / 961766 ="
[1] " 3.141592653514472 , error = -7.53206386150396e-11"
[1] "10 : 3959189 / 1260249 ="
[1] " 3.141592653515298 , error = -7.44955208631382e-11"
[1] "11 : 10939855 / 3482264 ="
[1] " 3.141592653515070 , error = -7.47237827170011e-11"
[1] "12 : 14899044 / 4742513 ="
[1] " 3.141592653515130 , error = -7.46629424952516e-11"
[1] "13 : 25838899 / 8224777 ="
[1] " 3.141592653515104 , error = -7.46886996694229e-11"
[1] "14 : 66576842 / 21192067 ="
[1] " 3.141592653515110 , error = -7.46829265096949e-11"
[1] "15 : 92415741 / 29416844 ="
[1] " 3.141592653515108 , error = -7.46847028665343e-11"
$num
[1] 3959189
$den
[1] 1260249
$error
[1] 7.449552e-11
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.