正如在Boost 多精度库 http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html文档,很容易从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>!
从 a 转换的能力cpp_int
到固定宽度浮点类型(即cpp_dec_float_50
)给了人们希望,即有可能从cpp_int
to an 任意宽度库中的浮点类型 - 即cpp_dec_float<0>
。然而,这是行不通的;在 Visual Studio 2013 中,转换失败,如以下简单示例程序所示:
#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
}
It does成功转换为cpp_dec_float_50
,如预期,但如前所述,我希望转换为任意精度浮点类型:cpp_dec_float<0>
.
该错误出现在文件中内部 Boost Multi precision 代码的以下代码片段中<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::表达式::result_type>,bool>::type
boost::多精度::运算符 >(const
boost::多精度::细节::表达式
&,常量
boost::多精度::细节::表达式 &)' :
无法推断出“const”的模板参数
boost::多精度::细节::表达式 &'
来自“const next_type”
是否可以转换boost::multiprecision::cpp_int
to a boost::multiprecision::cpp_dec_float<0>
(而不是转换为具有固定小数精度的浮点类型,如cpp_dec_float_50
)?
(请注意,在我的程序中,任何时候都只实例化一个浮点数实例,并且很少更新,因此我可以接受让这个实例占用大量内存并需要很长时间才能真正支持数量巨大。)
Thanks!