digits.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_DETAIL_DIGITS_HPP
  6. #define BOOST_MP_DETAIL_DIGITS_HPP
  7. namespace boost { namespace multiprecision { namespace detail {
  8. inline constexpr unsigned long digits10_2_2(unsigned long d10)
  9. {
  10. return (d10 * 1000uL) / 301uL + ((d10 * 1000uL) % 301 ? 2u : 1u);
  11. }
  12. inline constexpr unsigned long digits2_2_10(unsigned long d2)
  13. {
  14. return (d2 * 301uL) / 1000uL;
  15. }
  16. #if ULONG_MAX != SIZE_MAX
  17. inline constexpr std::size_t digits10_2_2(std::size_t d10)
  18. {
  19. return (d10 * 1000uL) / 301uL + ((d10 * 1000uL) % 301 ? 2u : 1u);
  20. }
  21. inline constexpr std::size_t digits2_2_10(std::size_t d2)
  22. {
  23. return (d2 * 301uL) / 1000uL;
  24. }
  25. template <class I>
  26. inline constexpr typename std::enable_if<sizeof(I) <= sizeof(unsigned long), unsigned long>::type digits10_2_2(I d10)
  27. {
  28. return digits10_2_2(static_cast<unsigned long>(d10));
  29. }
  30. template <class I>
  31. inline constexpr typename std::enable_if<sizeof(I) <= sizeof(unsigned long), unsigned long>::type digits2_2_10(I d10)
  32. {
  33. return digits2_2_10(static_cast<unsigned long>(d10));
  34. }
  35. #endif
  36. }}} // namespace boost::multiprecision::detail
  37. #endif