5

Boost Multiprecision ライブラリのドキュメントで明らかにされているように、 a から aboost::multiprecision::cpp_intへの変換は簡単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>!

acpp_intから固定幅浮動小数点型 (つまり、 a ) に変換できるということは、 aからライブラリ内の任意幅cpp_dec_float_50の浮動小数点型 (つまり、 a ) に変換できる可能性があるという 1 つの希望を与えてくれます。ただし、これは機能しません。次の簡単なサンプル プログラムが示すように、Visual Studio 2013 では変換が失敗します。cpp_intcpp_dec_float<0>

#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 Multiprecision コードからの次のコード スニペットに表示されます<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,detail::expression::result_type>,bool>::type boost::multiprecision::operator >(const boost::multiprecision::detail::expression &,const boost::multiprecision ::detail::expression &)': 'const boost::multiprecision::detail::expression &' のテンプレート引数を 'const next_type' から推測できませんでした

boost::multiprecision::cpp_intaを aに変換することは可能でしょうかboost::multiprecision::cpp_dec_float<0>(のように固定小数点精度の浮動小数点型に変換するのではなくcpp_dec_float_50)?

(私のプログラムでは、浮動小数点数のインスタンスが常に 1 つだけインスタンス化され、頻繁に更新されないことに注意してください。そのため、この 1 つのインスタンスが大量のメモリを消費し、実際にサポートするのに長い時間がかかることは問題ありません。膨大な数。)

ありがとう!

4

1 に答える 1

6

Boost Multiprecision の経験はあまりありませんが、テンプレート クラスはbackendcpp_dec_float<>と呼ばれるものであり、算術型として使用するにはアダプターでラップする必要があるようです。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's 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's 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);
于 2014-04-12T09:25:11.790 に答える