How to convert from boost :: multiprecision :: cpp_int to cpp_dec_float <0> (and not to cpp_dec_float_50, etc.)?

As shown in the Boost Multiprecision documentation , converting from boost::multiprecision::cpp_intto boost::multiprecision::cpp_dec_float:

// Some interconversions between number types are completely generic,
// and are always available, albeit the conversions are always explicit:

cpp_int cppi(2);
cpp_dec_float_50 df(cppi);    // OK, int to float // <-- But fails with cpp_dec_float<0>!

The ability to convert from cpp_intto a fixed-point floating-point type (i.e. a cpp_dec_float_50) gives hope that it would be possible to convert from a cpp_intto arbitrary width - the type of point in the library, i.e. a cpp_dec_float<0>. However, this does not work; the conversion failed for me in Visual Studio 2013, as shown in the following simple sample program:

#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

int main()
{
    boost::multiprecision::cpp_int n{ 0 };
    boost::multiprecision::cpp_dec_float<0> f{ n }; // Compile error in MSVC 2013
}

cpp_dec_float_50, , , , : cpp_dec_float<0>.

Boost <boost/multiprecision/detail/default_ops.hpp>:

template <class R, class T>
inline bool check_in_range(const T& t)
{
   // Can t fit in an R?
   if(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded
      && (t > (std::numeric_limits<R>::max)()))
      return true;
   return false;
}

:

C2784: "Enable_if:: result_type, :: :: result_type > BOOL > :: boost:: multiprecision:: operator > (const :: multiprecision:: :: & ;, Const boost:: multiprecision:: detail:: expression &) ': 'const boost:: multiprecision:: detail:: & ' from 'const next_type'

a boost::multiprecision::cpp_int boost::multiprecision::cpp_dec_float<0> ( , cpp_dec_float_50)?

( , , , , .)

!

+4
1

Boost Multiprecision, , cpp_dec_float<> - , backend, number<>, .

: Live On Coliru

#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>

namespace mp = boost::multiprecision;

int main()
{
    using Int = mp::cpp_int;

    // let think of a nice large number
    Int n = 1;
    for (Int f = 42; f>0; --f)
        n *= f;

    std::cout << n << "\n\n"; // print it for vanity 

    // let convert it to cpp_dec_float
    // and... do something with it
    using Dec = mp::number<mp::cpp_dec_float<0> >;
    std::cout << n.convert_to<Dec>();
}

:

1405006117752879898543142606244511569936384000000000

1.40501e+51

convert_to<>, , :

Dec decfloat(n);
+5

Source: https://habr.com/ru/post/1536214/


All Articles