...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/luroth_expansion.hpp> namespace boost::math::tools { template<typename Real, typename Z = int64_t> class luroth_expansion { public: luroth_expansion(Real x); std::vector<Z> const & digits() const; Real digit_geometric_mean() const; template<typename T, typename Z_> friend std::ostream& operator<<(std::ostream& out, luroth_expansion<T, Z_>& luroth); }; }
The luroth_expansion
class
provided by Boost expands a floating point number into a Lüroth representation,
i.e.,
The numbers di are called digits or denominators; we use the terminology digits, since technically in our notation d0 is not a denominator.
Here's a minimal working example:
using boost::math::constants::pi; using boost::math::tools::luroth_expansion; auto luroth = luroth_expansion(pi<long double>()); std::cout << "π ≈ " << luroth << "\n"; // Prints: // π ≈ ((3; 7, 1, 1, 1, 2, 1, 4, 23, 4, 1, 1, 1, 1, 80, 1, 1, 5))
The class computes denominators while simultaneously computing convergents. Once a convergent is within a few ulps of the input value, the computation stops.
Nota bene: There is an alternative definition of the Lüroth representation where every digit is shifted by 1. We follow the definition given in Kalpazidou; with the modification that we do not constrain the input to be in the interval [0,1] and let the first digit be the floor of the input.
For almost all real numbers, the geometric mean of the digits converges to a constant which is approximately 2.2001610580. This is "Khinchin's constant" for the Lüroth representation.