engel_expansion.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // (C) Copyright Nick Thompson 2020.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_ENGEL_EXPANSION_HPP
  6. #define BOOST_MATH_TOOLS_ENGEL_EXPANSION_HPP
  7. #include <vector>
  8. #include <ostream>
  9. #include <iomanip>
  10. #include <cmath>
  11. #include <limits>
  12. #include <stdexcept>
  13. namespace boost::math::tools {
  14. template<typename Real, typename Z = int64_t>
  15. class engel_expansion {
  16. public:
  17. engel_expansion(Real x) : x_{x}
  18. {
  19. using std::floor;
  20. using std::abs;
  21. using std::sqrt;
  22. using std::isfinite;
  23. if (!isfinite(x))
  24. {
  25. throw std::domain_error("Cannot convert non-finites into an Engel expansion.");
  26. }
  27. if(x==0)
  28. {
  29. throw std::domain_error("Zero does not have an Engel expansion.");
  30. }
  31. a_.reserve(64);
  32. // Let the error bound grow by 1 ULP/iteration.
  33. // I haven't done the error analysis to show that this is an expected rate of error growth,
  34. // but if you don't do this, you can easily get into an infinite loop.
  35. Real i = 1;
  36. Real computed = 0;
  37. Real term = 1;
  38. Real scale = std::numeric_limits<Real>::epsilon()*abs(x_)/2;
  39. Real u = x;
  40. while (abs(x_ - computed) > (i++)*scale)
  41. {
  42. Real recip = 1/u;
  43. Real ak = ceil(recip);
  44. a_.push_back(static_cast<Z>(ak));
  45. u = u*ak - 1;
  46. if (u==0)
  47. {
  48. break;
  49. }
  50. term /= ak;
  51. computed += term;
  52. }
  53. for (size_t i = 1; i < a_.size(); ++i)
  54. {
  55. // Sanity check: This should only happen when wraparound occurs:
  56. if (a_[i] < a_[i-1])
  57. {
  58. throw std::domain_error("The digits of an Engel expansion must form a non-decreasing sequence; consider increasing the wide of the integer type.");
  59. }
  60. // Watch out for saturating behavior:
  61. if (a_[i] == std::numeric_limits<Z>::max())
  62. {
  63. throw std::domain_error("The integer type Z does not have enough width to hold the terms of the Engel expansion; please widen the type.");
  64. }
  65. }
  66. a_.shrink_to_fit();
  67. }
  68. const std::vector<Z>& digits() const
  69. {
  70. return a_;
  71. }
  72. template<typename T, typename Z2>
  73. friend std::ostream& operator<<(std::ostream& out, engel_expansion<T, Z2>& eng);
  74. private:
  75. Real x_;
  76. std::vector<Z> a_;
  77. };
  78. template<typename Real, typename Z2>
  79. std::ostream& operator<<(std::ostream& out, engel_expansion<Real, Z2>& engel)
  80. {
  81. constexpr const int p = std::numeric_limits<Real>::max_digits10;
  82. if constexpr (p == 2147483647)
  83. {
  84. out << std::setprecision(engel.x_.backend().precision());
  85. }
  86. else
  87. {
  88. out << std::setprecision(p);
  89. }
  90. out << "{";
  91. for (size_t i = 0; i < engel.a_.size() - 1; ++i)
  92. {
  93. out << engel.a_[i] << ", ";
  94. }
  95. out << engel.a_.back();
  96. out << "}";
  97. return out;
  98. }
  99. }
  100. #endif