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
6#ifndef BOOST_MP_MAX_DIGITS10_HPP
7#define BOOST_MP_MAX_DIGITS10_HPP
8
9namespace boost {
10namespace multiprecision {
11namespace detail {
12
13template <unsigned digits>
14struct calc_max_digits10
15{
16 static constexpr unsigned max_digits_10(unsigned d)
17 {
18 //
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:
24 //
25 return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * d) + 2;
26 }
27 static constexpr unsigned value = max_digits_10(d: digits);
28};
29
30template <std::size_t digits>
31struct calc_max_digits10_s
32{
33 static constexpr std::size_t max_digits_10(std::size_t d)
34 {
35 //
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:
41 //
42 return static_cast<std::size_t>(static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d)) + 2u);
43 }
44 static constexpr std::size_t value = max_digits_10(d: digits);
45};
46
47template <unsigned digits>
48struct calc_digits10
49{
50 static constexpr unsigned digits_10(unsigned d)
51 {
52 //
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.
56 //
57 return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
58 }
59 static constexpr unsigned value = digits_10(d: digits);
60};
61
62template <std::size_t digits>
63struct calc_digits10_s
64{
65 static constexpr std::size_t digits_10(std::size_t d)
66 {
67 //
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.
71 //
72 return static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
73 }
74 static constexpr std::size_t value = digits_10(d: digits);
75};
76
77}}} // namespace boost::multiprecision::detail
78
79#endif
80

source code of boost/libs/multiprecision/include/boost/multiprecision/traits/max_digits10.hpp