Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:42:24

0001 ///////////////////////////////////////////////////////////////
0002 //  Copyright 2012 John Maddock. Distributed under the Boost
0003 //  Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
0005 
0006 #ifndef BOOST_MP_MAX_DIGITS10_HPP
0007 #define BOOST_MP_MAX_DIGITS10_HPP
0008 
0009 namespace boost {
0010 namespace multiprecision {
0011 namespace detail {
0012 
0013 template <unsigned digits>
0014 struct calc_max_digits10
0015 {
0016    static constexpr unsigned max_digits_10(unsigned d)
0017    {
0018       //
0019       // We need ceil(log10(2) * d) + 1 decimal places to
0020       // guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
0021       // and references therein.  Since log10(2) is irrational, then d*log10(2) will
0022       // never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
0023       // and avoid the call to ceil:
0024       //
0025       return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * d) + 2;
0026    }
0027    static constexpr unsigned value = max_digits_10(digits);
0028 };
0029 
0030 template <std::size_t digits>
0031 struct calc_max_digits10_s
0032 {
0033    static constexpr std::size_t max_digits_10(std::size_t d)
0034    {
0035       //
0036       // We need ceil(log10(2) * d) + 1 decimal places to
0037       // guarantee round tripping, see: https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
0038       // and references therein.  Since log10(2) is irrational, then d*log10(2) will
0039       // never be exactly an integer so we can replace by trunc(log10(2) * d) + 2
0040       // and avoid the call to ceil:
0041       //
0042       return static_cast<std::size_t>(static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d)) + 2u);
0043    }
0044    static constexpr std::size_t value = max_digits_10(digits);
0045 };
0046 
0047 template <unsigned digits>
0048 struct calc_digits10
0049 {
0050    static constexpr unsigned digits_10(unsigned d)
0051    {
0052       //
0053       // We need floor(log10(2) * (d-1)), see: 
0054       // https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
0055       // and references therein.
0056       //
0057       return static_cast<unsigned>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
0058    }
0059    static constexpr unsigned value = digits_10(digits);
0060 };
0061 
0062 template <std::size_t digits>
0063 struct calc_digits10_s
0064 {
0065    static constexpr std::size_t digits_10(std::size_t d)
0066    {
0067       //
0068       // We need floor(log10(2) * (d-1)), see: 
0069       // https://www.exploringbinary.com/number-of-digits-required-for-round-trip-conversions/
0070       // and references therein.
0071       //
0072       return static_cast<std::size_t>(0.301029995663981195213738894724493026768189881462108541310 * static_cast<double>(d - 1u));
0073    }
0074    static constexpr std::size_t value = digits_10(digits);
0075 };
0076 
0077 }}} // namespace boost::multiprecision::detail
0078 
0079 #endif