gpt4 book ai didi

c++ - boost::multiprecision 从整数转换为 cpp_dec_float 编译错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:52:42 25 4
gpt4 key购买 nike

我在将一些代码移动到服务器时遇到问题。

该问题的一个最小工作示例是以下代码(文件名 mp2.cpp):

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

using namespace std;
using namespace boost::multiprecision;

int main() {
uint128_t a = 1;
uint128_t b = 2;
cpp_dec_float_50 fa = static_cast<cpp_dec_float_50>(a);
cpp_dec_float_50 fb = static_cast<cpp_dec_float_50>(b);
cout << fa / fb << endl;
return 0;
}

按预期编译和工作,在我的开发机器(g++ 4.6.3,boost 1.55,32 位 CPU)的标准输出上写入 0.5

对于相同的代码,g++ 在服务器上编译失败并出现此错误:

In file included from /usr/local/include/boost/multiprecision/number.hpp:22:0,
from /usr/local/include/boost/multiprecision/cpp_int.hpp:12,
from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp: In instantiation of 'void boost::multiprecision::detail::generic_interconvert(To&, const From&, const mpl_::int_<1>&, const mpl_::int_<0>&) [with To = boost::multiprecision::backends::cpp_dec_float<50u>; From = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>]':
/usr/local/include/boost/multiprecision/number.hpp:108:7: required from 'boost::multiprecision::number<Backend, ExpressionTemplates>::number(const boost::multiprecision::number<Other, ET>&, typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type*) [with Other = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>; boost::multiprecision::expression_template_option ET = (boost::multiprecision::expression_template_option)0u; Backend = boost::multiprecision::backends::cpp_dec_float<50u>; boost::multiprecision::expression_template_option ExpressionTemplates = (boost::multiprecision::expression_template_option)1u; typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type = void]'
mp2.cpp:11:55: required from here
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: error: no matching function for call to 'eval_is_zero(boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>&)'
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: note: candidates are:
In file included from mp2.cpp:2:0:
/usr/local/include/boost/multiprecision/cpp_dec_float.hpp:2905:13: note: template<unsigned int Digits10, class ExponentType, class Allocator> bool boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::cpp_dec_float<Digits10, ExponentType, Allocator>&)
/usr/local/include/boost/multiprecision/cpp_dec_float.hpp:2905:13: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/multiprecision/number.hpp:22:0,
from /usr/local/include/boost/multiprecision/cpp_int.hpp:12,
from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: note: 'boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>' is not derived from 'const boost::multiprecision::backends::cpp_dec_float<Digits10, ExponentType, Allocator>'
In file included from /usr/local/include/boost/multiprecision/cpp_int.hpp:1787:0,
from mp2.cpp:1:
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp:94:4: note: template<unsigned int MinBits1, unsigned int MaxBits1, boost::multiprecision::cpp_integer_type SignType1, boost::multiprecision::cpp_int_check_type Checked1, class Allocator1> typename boost::enable_if_c<(! boost::multiprecision::backends::is_trivial_cpp_int<boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value), bool>::type boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>&)
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp:94:4: note: template argument deduction/substitution failed:
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp: In substitution of 'template<unsigned int MinBits1, unsigned int MaxBits1, boost::multiprecision::cpp_integer_type SignType1, boost::multiprecision::cpp_int_check_type Checked1, class Allocator1> typename boost::enable_if_c<(! boost::multiprecision::backends::is_trivial_cpp_int<boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value), bool>::type boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>&) [with unsigned int MinBits1 = 128u; unsigned int MaxBits1 = 128u; boost::multiprecision::cpp_integer_type SignType1 = (boost::multiprecision::cpp_integer_type)0u; boost::multiprecision::cpp_int_check_type Checked1 = (boost::multiprecision::cpp_int_check_type)0u; Allocator1 = void]':
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: required from 'void boost::multiprecision::detail::generic_interconvert(To&, const From&, const mpl_::int_<1>&, const mpl_::int_<0>&) [with To = boost::multiprecision::backends::cpp_dec_float<50u>; From = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>]'
/usr/local/include/boost/multiprecision/number.hpp:108:7: required from 'boost::multiprecision::number<Backend, ExpressionTemplates>::number(const boost::multiprecision::number<Other, ET>&, typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type*) [with Other = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>; boost::multiprecision::expression_template_option ET = (boost::multiprecision::expression_template_option)0u; Backend = boost::multiprecision::backends::cpp_dec_float<50u>; boost::multiprecision::expression_template_option ExpressionTemplates = (boost::multiprecision::expression_template_option)1u; typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type = void]'
mp2.cpp:11:55: required from here
/usr/local/include/boost/multiprecision/cpp_int/misc.hpp:94:4: error: no type named 'type' in 'struct boost::enable_if_c<false, bool>'
In file included from /usr/local/include/boost/multiprecision/cpp_int.hpp:18:0,
from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp: In instantiation of 'void boost::multiprecision::detail::generic_interconvert(To&, const From&, const mpl_::int_<1>&, const mpl_::int_<0>&) [with To = boost::multiprecision::backends::cpp_dec_float<50u>; From = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>]':
/usr/local/include/boost/multiprecision/number.hpp:108:7: required from 'boost::multiprecision::number<Backend, ExpressionTemplates>::number(const boost::multiprecision::number<Other, ET>&, typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type*) [with Other = boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>; boost::multiprecision::expression_template_option ET = (boost::multiprecision::expression_template_option)0u; Backend = boost::multiprecision::backends::cpp_dec_float<50u>; boost::multiprecision::expression_template_option ExpressionTemplates = (boost::multiprecision::expression_template_option)1u; typename boost::enable_if_c<(! boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>::value)>::type = void]'
mp2.cpp:11:55: required from here
/usr/local/include/boost/multiprecision/rational_adaptor.hpp:252:13: note: template<class IntBackend> bool boost::multiprecision::backends::eval_is_zero(const boost::multiprecision::backends::rational_adaptor<IntBackend>&)
/usr/local/include/boost/multiprecision/rational_adaptor.hpp:252:13: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/multiprecision/number.hpp:22:0,
from /usr/local/include/boost/multiprecision/cpp_int.hpp:12,
from mp2.cpp:1:
/usr/local/include/boost/multiprecision/detail/generic_interconvert.hpp:43:4: note: 'boost::multiprecision::backends::cpp_int_backend<128u, 128u, (boost::multiprecision::cpp_integer_type)0u, (boost::multiprecision::cpp_int_check_type)0u, void>' is not derived from 'const boost::multiprecision::backends::rational_adaptor<IntBackend>'

服务器在 64 位 CPU 上运行 g++ 4.7.2 和 boost 1.56。我已经安装了相同的 g++ 和 boost 版本以查看这是否是问题所在,但事实并非如此。

通过对源代码进行试验,每当我尝试将整数转换为 float 时就会出现问题。然而,如前所述in the documentation ,这是执行此操作的正确方法。

有人知道问题出在哪里,以及如何解决吗?

最佳答案

我不知道这个库,但问题似乎来自 x86-64 架构的错误(?)。

需要注意的是:

  1. 我的 64 位操作系统(Linux Mint 16 - g++ 4.8.1 - boost 1.53)也有同样的问题。
  2. 作为解决方法,您可以使用 -m32 标志将代码交叉编译为 32 位二进制文​​件(已成功测试)。
  3. 使用 cpp_int 而不是 uint128_t 问题消失了。
  4. 奇怪的是,使用以下自定义类型没有出现编译错误:

    typedef number<cpp_int_backend<128, 128, signed_magnitude, unchecked, std::allocator<limb_type> > > t128_custom;

    当此分配器替换为 void(由 uint128_t 定义)或使用 usigned_magnitude 时,会出现问题。

也许您应该联系 boost 社区以获取更多信息。

关于c++ - boost::multiprecision 从整数转换为 cpp_dec_float 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25768131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com