本文介绍了如何从boost :: multiprecision :: cpp_int转换为cpp_dec_float< 0> (而不是cpp_dec_float_50等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

正如文档,可以直接从 boost :: multiprecision :: cpp_int 转换为 boost :: multiprecision :: cpp_dec_float

As is made clear in the Boost Multiprecision library documentation, it is straightforward to convert from a boost::multiprecision::cpp_int to a 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>!

能够从 cpp_int 一个固定宽度浮点类型(即 cpp_dec_float_50 )希望可以从 cpp_int 到库中的任意宽度浮点类型 - 即 cpp_dec_float< 0> 。然而,这不工作;在Visual Studio 2013中转换失败,如下面的简单示例程序所示:

The ability to convert from a cpp_int to a fixed-width floating-point type (i.e., a cpp_dec_float_50) gives one hope that it might be possible to convert from a cpp_int to an arbitrary-width floating-point type in the library - i.e., a cpp_dec_float<0>. However, this doesn't work; the conversion fails for me in Visual Studio 2013, as the following simple example program demonstrates:

#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& / code>。

It does succeed to convert to cpp_dec_float_50, as expected, but as noted, I am hoping to convert to an arbitrary precision floating point type: cpp_dec_float<0>.

错误出现在内部Boost多精度代码的以下代码段中,位于文件< boost /multiprecision/detail/default_ops.hpp>

The error appear in the following snippet of code from the internal Boost Multiprecision code, in the file <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> (const
boost :: multiprecision :: detail :: expression
&,const
boost :: multiprecision :: detail :: expression&)':
无法推导模板参数'const
boost :: multiprecision :: detail :: expression&'
from'const next_type'

可以将 boost :: multiprecision :: cpp_int 转换为 boost :: multiprecision :: cpp_dec_float< 0> / code>(而不是转换为具有固定小数精度的浮点类型,如 cpp_dec_float_50 )?

(注意,在我的程序中,只有一个浮点数实例在任何时候被实例化,并且它很少更新,所以我很高兴这个实例占用大量内存,很长时间来支持真正巨大的数字。)

(Note that in my program, only one instance of the floating-point number is instantiated at any time, and it is updated infrequently, so I am fine with having this one instance take up lots of memory and take a long time to support really huge numbers.)

谢谢!

推荐答案

我对Boost Multiprecision没有多少经验,但在我看来,模板类 cpp_dec_float<> 是他们称为后端,您需要将它包装在数字<> 适配器中,以便将其用作算术类型。

I don't have much experience with Boost Multiprecision, but it seems to me that the template class cpp_dec_float<> is what they call a backend, and you need to wrap it in a number<> adaptor in order to use it as an arithmetic type.

以下是我对此的介绍:

Here's my take on it: 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<> ,则显式转换构造函数也可以使用,我希望:

If convert_to<> is allowed, then the explicit conversion constructor will also work, I expect:

Dec decfloat(n);

这篇关于如何从boost :: multiprecision :: cpp_int转换为cpp_dec_float&lt; 0&gt; (而不是cpp_dec_float_50等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:46