如何从boost :: multiprecision :: cpp_int转换为cpp_dec_float< 0& gt;(而不是cpp_dec_float_50等)? [英] How to convert from boost::multiprecision::cpp_int to cpp_dec_float<0> (rather than to cpp_dec_float_50, etc.)?

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

问题描述

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> .

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 Multiprecision代码的以下代码段中,文件< 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> :: typeboost :: multiprecision :: operator>(constboost :: multiprecision :: detail :: expression&,constboost :: multiprecision :: detail :: expression&)':无法推导'const的模板参数boost :: multiprecision :: detail :: expression&'来自"const next_type"

error C2784: 'enable_if::result_type,detail::expression::result_type>,bool>::type boost::multiprecision::operator >(const boost::multiprecision::detail::expression &,const boost::multiprecision::detail::expression &)' : could not deduce template argument for 'const boost::multiprecision::detail::expression &' from 'const next_type'

可能可以将 boost :: multiprecision :: cpp_int 转换为 boost :: multiprecision :: cpp_dec_float< 0> (而不是转换为浮点型点的类型具有固定的十进制精度,例如 cpp_dec_float_50 )?

Might it be possible to convert a boost::multiprecision::cpp_int to a boost::multiprecision::cpp_dec_float<0> (rather than converting to a floating-point type with a fixed decimal precision, as in 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<> 是什么他们称为后端,并且您需要将其包装在 number<> 适配器中,才能将其用作算术类型.

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.

这是我的看法: 在Coliru上直播

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&amp; gt;(而不是cpp_dec_float_50等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆