max_digits10.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_MP_MAX_DIGITS10_HPP
  6. #define BOOST_MP_MAX_DIGITS10_HPP
  7. namespace boost {
  8. namespace multiprecision {
  9. namespace detail {
  10. template <unsigned digits>
  11. struct calc_max_digits10
  12. {
  13. static constexpr unsigned max_digits_10(unsigned d)
  14. {
  15. //
  16. // We need ceil(log10(2) * d) + 1 decimal places to
  17. // guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
  18. // and references therein. Since log10(2) is irrational, then d*log10(2) will
  19. // never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
  20. // and avoid the call to ceil:
  21. //
  22. return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * d) + 2;
  23. }
  24. static constexpr unsigned value = max_digits_10(digits);
  25. };
  26. template <std::size_t digits>
  27. struct calc_max_digits10_s
  28. {
  29. static constexpr std::size_t max_digits_10(std::size_t d)
  30. {
  31. //
  32. // We need ceil(log10(2) * d) + 1 decimal places to
  33. // guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
  34. // and references therein. Since log10(2) is irrational, then d*log10(2) will
  35. // never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
  36. // and avoid the call to ceil:
  37. //
  38. return static_cast<std::size_t>(static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d)) + 2u);
  39. }
  40. static constexpr std::size_t value = max_digits_10(digits);
  41. };
  42. template <unsigned digits>
  43. struct calc_digits10
  44. {
  45. static constexpr unsigned digits_10(unsigned d)
  46. {
  47. //
  48. // We need floor(log10(2) * (d-1)), see:
  49. // https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
  50. // and references therein.
  51. //
  52. return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
  53. }
  54. static constexpr unsigned value = digits_10(digits);
  55. };
  56. template <std::size_t digits>
  57. struct calc_digits10_s
  58. {
  59. static constexpr std::size_t digits_10(std::size_t d)
  60. {
  61. //
  62. // We need floor(log10(2) * (d-1)), see:
  63. // https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
  64. // and references therein.
  65. //
  66. return static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
  67. }
  68. static constexpr std::size_t value = digits_10(digits);
  69. };
  70. }}} // namespace boost::multiprecision::detail
  71. #endif