Given a vector of denominators and optionally a vector of numerators, calculate the bigq
fraction and the mpfr
extended precision decimal value.
cfrac2num(denom, num = 1, init = 0, nreps= 1, msgrate = NULL )
The exact numeric value is provided in cgmp
, a bigq
fraction. The mpfr
value is calculated using .bigq2mpfr
, which generates a decimal number of sufficient precision to match the implied precision of the fraction.
fracpart
equals cgmp
minus the integer part, if any.
denom
and num
provide the vectors of denominator and numerator values used in the calculation.
A vector in standard continued fraction form [b0; a1,a2,a3,...]
where b0
represents the integer part of the number and the a_j
are the denominator terms. If all terms, including b0
, are the same, optionally enter a single value and specify the length with the numterms
value
A vector of numerator values. If all numerators are the same, a single value will suffice. The default is 1
In those cases where the denominator values repeat (e.g., for continued fraction form of square roots), it is possible to take the output of a previous run and use it to initialize a new run. This input must be a bigq
fraction. Use with care.
If denoms
is a single value, repeat that value nreps
times to set up the continued fraction. Otherwise, assume a periodic denominator sequence was submitted, and repeat the denom[-1]
section nreps
times (the first denominator is the integer term) .
If desired enter an integer indicating how often a status message should be displayed. Leave as NULL
to suppress messages. For example, setting thi to 10 will generate a message every 10 sub-fraction reductions.
Carl Witthoft, carl@witthoft.com
All calculations are done with bigq
fractions to preserve full precision. Naturally, a finite input will not yield the exact value for any irrational number.
num2cfrac
cfrac2num(rep(1,10)) # approximate phi
frac2 <- sqrt2periodicCfrac(2)
cfrac2num(frac2$numericdenom)
#simple cases
cfrac2num(denom=1)
cfrac2num(denom = c(0,2),num=1)
Run the code above in your browser using DataLab