...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#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
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 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.