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
6 #ifndef BOOST_MATH_MAX_DIGITS10_HPP
7 #define BOOST_MATH_MAX_DIGITS10_HPP
10 namespace multiprecision {
13 template <unsigned digits>
14 struct calc_max_digits10
16 static constexpr unsigned max_digits_10(unsigned d)
19 // We need ceil(log10(2) * d) + 1 decimal places to
20 // guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
21 // and references therein. Since log10(2) is irrational, then d*log10(2) will
22 // never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
23 // and avoid the call to ceil:
25 return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * d) + 2;
27 static constexpr const unsigned value = max_digits_10(digits);
30 template <std::size_t digits>
31 struct calc_max_digits10_s
33 static constexpr std::size_t max_digits_10(std::size_t d)
36 // We need ceil(log10(2) * d) + 1 decimal places to
37 // guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
38 // and references therein. Since log10(2) is irrational, then d*log10(2) will
39 // never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
40 // and avoid the call to ceil:
42 return static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * d) + 2;
44 static constexpr const std::size_t value = max_digits_10(digits);
47 template <unsigned digits>
50 static constexpr unsigned digits_10(unsigned d)
53 // We need floor(log10(2) * (d-1)), see:
54 // https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
55 // and references therein.
57 return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * (d - 1));
59 static constexpr const unsigned value = digits_10(digits);
62 template <std::size_t digits>
63 struct calc_digits10_s
65 static constexpr std::size_t digits_10(std::size_t d)
68 // We need floor(log10(2) * (d-1)), see:
69 // https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
70 // and references therein.
72 return static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * (d - 1));
74 static constexpr const std::size_t value = digits_10(digits);
77 }}} // namespace boost::multiprecision::detail