我看到与boost multiprecision库有些不一致,使用cpp_int并且想知道问题是否在我的最后?我做错了吗?
boost::multiprecision::cpp_int value("845812507058753702096720396260955981034309941487979439207575316627396775257009179367680598562088782400182102510047921049667535737841056751035898984440045398065941794853342721440022891483618946596390530332584847468817849746783423105644934675762519035784877729169739110084935079201004991911753548016158266946593610497793934212345180527788034865286995713462176706647193473406223095268503330593499438446017000593156395272905592017851490768402042283892535127698736772114426168690580061412400354553387531076676433901465842118416610671452446364936252601684680593015917270112975907856081311621268680168563153055479531193987696015767888543608430149655940111761214342848772129089336344636193634262254610730");
boost::multiprecision::cpp_int residueResult = value % 733;
std::cout << residueResult; // this prints out 4
int residue1 = residueResult.convert_to<int>(); // this is 4
int residue2 = int(value % 733); // this is 1
为什么在执行int(值%733)时,它给出的值为1?
这是使用boost 1.59.0和visual studio 2013社区。 p>
答案 0 :(得分:1)
第二次演员表无效,不应该编译。
test.cpp|9 col 35| error: invalid cast from type ‘boost::enable_if_c<true, boost::multiprecision::detail::expression<boost::multiprecision::detail::modulus_immediates, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<> >, int, void, void> >::type {aka boost::multiprecision::detail::expression<boost::multiprecision::detail::modulus_immediates, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<> >, int, void, void>}’ to type ‘int’
|| int residue2 = int(value % 733); // this is 1
test.cpp|9 col 20| error: cannot convert 'typename enable_if_c<is_compatible_arithmetic_type<int, number<cpp_int_backend<0, 0, boost::multiprecision::cpp_integer_type::signed_magnitude, boost::multiprecision::cpp_int_check_type::unchecked, allocator<unsigned long long> >, et_on> >::value && (number_category<cpp_int_backend<0, 0, boost::multiprecision::cpp_integer_type::signed_magnitude, boost::multiprecision::cpp_int_check_type::unchecked, allocator<unsigned long long> > >::value == number_kind_integer), detail::expression<detail::modulus_immediates, number<cpp_int_backend<0, 0, boost::multiprecision::cpp_integer_type::signed_magnitude, boost::multiprecision::cpp_int_check_type::unchecked, allocator<unsigned long long> >, et_on>, int> >::type' (aka 'boost::multiprecision::detail::expression<boost::multiprecision::detail::modulus_immediates, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0, 0, boost::multiprecision::cpp_integer_type::signed_magnitude, boost::multiprecision::cpp_
|| nt_check_type::unchecked, std::__1::allocator<unsigned long long> >, boost::multiprecision::expression_template_option::et_on>, int, void, void>') to 'int' without a conversion operator
|| int residue2 = int(value % 733); // this is 1
|| ^~~~~~~~~~~~~~~
如果你实际上写的不同,那么你可能已经得到了reinterpret_cast,因此结果是实现定义的。
评论:
<强> Live On Coliru 强>
#include <boost/multiprecision/cpp_int.hpp>
#include <cstdio>
int main() {
using Int = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>, boost::multiprecision::et_off>;
Int value("845812507058753702096720396260955981034309941487979439207575316627396775257009179367680598562088782400182102510047921049667535737841056751035898984440045398065941794853342721440022891483618946596390530332584847468817849746783423105644934675762519035784877729169739110084935079201004991911753548016158266946593610497793934212345180527788034865286995713462176706647193473406223095268503330593499438446017000593156395272905592017851490768402042283892535127698736772114426168690580061412400354553387531076676433901465842118416610671452446364936252601684680593015917270112975907856081311621268680168563153055479531193987696015767888543608430149655940111761214342848772129089336344636193634262254610730");
printf("residue %d:\n", int(value % 733));
}
打印
residue 4:
答案 1 :(得分:1)
目前的问题是value % 733
是一些实现细节代理类型,您不能将其转换为int
。实际上,这甚至都没有用gcc和clang编译,正如你所看到的那样,它会产生无意义的MSVC。要解决这个问题,请在投射前转换回cpp_int
:
int main () {
boost::multiprecision::cpp_int value("845812507058753702096720396260955981034309941487979439207575316627396775257009179367680598562088782400182102510047921049667535737841056751035898984440045398065941794853342721440022891483618946596390530332584847468817849746783423105644934675762519035784877729169739110084935079201004991911753548016158266946593610497793934212345180527788034865286995713462176706647193473406223095268503330593499438446017000593156395272905592017851490768402042283892535127698736772114426168690580061412400354553387531076676433901465842118416610671452446364936252601684680593015917270112975907856081311621268680168563153055479531193987696015767888543608430149655940111761214342848772129089336344636193634262254610730");
boost::multiprecision::cpp_int residueResult = value % 733;
std::cout << residueResult << "\n"; // this prints out 4
int residue1 = residueResult.convert_to<int>(); // this is 4
int residue2 = static_cast<int>(boost::multiprecision::cpp_int(value % 733));
std::cout << residue1 << "\n" << residue2;
}
这将打印
4
4
4
根据需要。