boost/libs/math/doc/internals/centered_continued_fraction.qbk
2021-10-05 21:37:46 +02:00

56 lines
2.1 KiB
Plaintext

[/
Copyright Nick Thompson, 2020
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt).
]
[section:centered_continued_fraction Centered Continued Fractions]
#include <boost/math/tools/centered_continued_fraction.hpp>
namespace boost::math::tools {
template<typename Real, typename Z = int64_t>
class centered_continued_fraction {
public:
centered_continued_fraction(Real x);
Real khinchin_geometric_mean() const;
template<typename T, typename Z_>
friend std::ostream& operator<<(std::ostream& out, centered_continued_fraction<T, Z_>& ccf);
};
}
The `centered_continued_fraction` class provided by Boost affords the ability to convert a floating point number into a centered continued fraction.
Unlike the simple continued fraction, a centered continued fraction allows partial denominators to be negative.
Here's a minimal working example:
using boost::math::constants::pi;
using boost::math::tools::centered_continued_fraction;
auto cfrac = centered_continued_fraction(pi<long double>());
std::cout << "π ≈ " << cfrac << "\n";
// Prints:
// π ≈ [3; 7, 16, -294, 3, -4, 5, -15, -3, 2, 2]
This function achieves the same end as the Maple syntax
[$../images/maple_cfrac.png]
You should convince yourself that the Maple output and the notation we use are in fact the same thing.
The class computes partial denominators which simultaneously computing convergents with the modified Lentz's algorithm.
Once a convergent is within a few ulps of the input value, the computation stops.
Just like simple continued fractions, centered continued fractions admit a "Khinchin constant".
The value of this constant is ~5.454517 (see [@http://jeremiebourdon.free.fr/data/Khintchine.pdf here]).
We can evaluate the "empirical Khinchin constant" for a particular number via
cfrac.khinchin_geometric_mean();
If this is close to 5.454517, then the number is probably uninteresting with respect to its characterization as a centered continued fraction.
[endsect]