To create a "simple" continued fraction, like \(e\), just call the Cfraction constructor.
1
consteuler=newCfraction([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
consteuler=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
consteuler_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
consteuler_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.