Continued Fractions

December 30th, 2016

A Quick Guide to my Continued Fraction Library


In general the continued fraction
can be constructed like so:
1
const myNumber = new Cfraction([a_0,a_1,a_2,a_3,a_4],[b_0,b_1,b_2,b_3,b_4]);
To create a "simple" continued fraction, like \(e\), just call the Cfraction constructor.
1
const euler = new Cfraction([2,1,2,1,1,4,1,1,8]);
On the other hand, you could use the built in E method to generate the first 9 terms for \(e\)
1
const euler = Cfraction.E(9);
If you want to return the \(\LaTeX\) code for your continued fraction, just use
1
euler.toTex();
which will return the string
Note that \cfrac{}{} is included in the amsmath package if you are trying to write directly to a tex file. Now if you want the decimal expansion (as a string), to say 1000 decimal places, just write
1
euler.decimal(1000);
If you want to find bounds on the error, just write
1
euler.error(20);
This will return the first 20 decimal places or whenever the first nonzero digit appears. Note that this method applies only to simple continued fractions. You can also get the error in scientific notation by calling
1
euler.error_sci(20);
Bringing this all together:
which has an error of
Now if you want \(e^{1/n}\), just use
1
const euler_nth_root = Cfraction.E_nth_root(9,n);
For example, the square root of \(e\) using Cfraction.E_nth_root(9,2).
If you need a weird power \(e^{x/y}\), use
1
const euler_power = Cfraction.E_power(9,x,y);
For example \(e^{5/2}\)
Cfraction.PI() will return the fixed value
We also have the log function, so Cfraction.LOG(9, 2, 1) gives \(\log(\frac{2}{1})\) or
We can compute \(\sqrt{7}\) easily with Cfraction.SQRT(9, 7),

Gosper's algorithm


Thanks to Bill Gosper, we can now actually use continued fractions for essentially infinite precision computation! To use his own example, suppose we want to find the continued fraction expansion of \(\tanh(\frac{1}{2})=\frac{e-1}{e+1}\) given that we already have the expansion for \(e\). In general, the expansion of \(\frac{ax+b}{cx+d}\) can be found by calling x.general_2d(a,b,c,d) So calling euler.general_2d(1,-1,1,1) gives

This allows us to compute things like \(\pi+\frac{5}{7}\), and with a slight modification of his original algorithm we can use this algorithm with generalized continued fractions!
As you can see expansions for \(\pi\) with patterns tend to converge quite slowly, which is why I have a fixed value.