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

45 lines
1.4 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:engel_expansion Engel Expansion]
#include <boost/math/tools/engel_expansion.hpp>
namespace boost::math::tools {
template<typename Real, typename Z = int64_t>
class engel_expansion {
public:
engel_expansion(Real x);
std::vector<Z> const & digits() const;
template<typename T, typename Z_>
friend std::ostream& operator<<(std::ostream& out, engel_expansion<T, Z>& engel);
};
}
The `engel_expansion` class provided by Boost converts a floating point number into an [@https://en.wikipedia.org/wiki/Engel_expansion Engel series].
Here's a minimal working example:
using boost::math::constants::e;
using boost::math::tools::engel_expansion;
auto engel = engel_expansion(e<double>());
std::cout << "e ≈ " << engel << "\n";
// Prints:
// e ≈ {1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
The digits of an Engel expansion tend to grow exponentially, so the integer template option might need to be utilized.
For example, we can use a wider integer type via
using boost::multiprecision::checked_int1024_t;
auto engel = engel_expansion<__float128, checked_int1024_t>(pi<__float128>());
[endsect]