C ++ Boost multiprecision cpp_int

时间:2014-02-11 08:31:04

标签: c++ boost

我试着记录一个大数字。我该怎么办?我不能使用gmp.hpp,因为它说Cannot open include file: 'gmp.h': No such file or directory

以下代码

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

#define rsa100 "1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139"


using namespace std;
using namespace boost::multiprecision;

int main(){
    cpp_int n(rsa100);
    cout << boost::multiprecision::log(n);
    return 0;
}

给我错误:

1>------ Build started: Project: rsa, Configuration: Debug x64 ------
1>  Source.cpp
1>Source.cpp(12): error C2893: Failed to specialize function template 'enable_if_c<boost::multiprecision::number_category<Num>::value==1,boost::multiprecision::detail::expression<boost::multiprecision::detail::function,boost::multiprecision::detail::log_funct<Backend>,boost::multiprecision::number<B,et_on>,void,void>>::type boost::multiprecision::log(const boost::multiprecision::number<B,et_on> &)'
1>          With the following template arguments:
1>          'Backend=boost::multiprecision::backends::cpp_int_backend<0,0,signed_magnitude,unchecked,std::allocator<boost::multiprecision::limb_type>>'
1>Source.cpp(12): error C2784: 'enable_if_c<boost::multiprecision::number_category<boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4>>::value==number_kind_floating_point,boost::multiprecision::detail::expression<boost::multiprecision::detail::function,boost::multiprecision::detail::log_funct<detail::backend_type<boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4>>::type>,boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4>,void,void>>::type boost::multiprecision::log(const boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4> &)' : could not deduce template argument for 'const boost::multiprecision::detail::expression<tag,Arg1,Arg2,Arg3,Arg4> &' from 'boost::multiprecision::cpp_int'
1>          C:\boost_1_55_0\boost/multiprecision/detail/default_ops.hpp(1998) : see declaration of 'boost::multiprecision::log'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:3)

log取实数,而不是整数。

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

#define rsa100 "1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139"

using namespace std;
using namespace boost::multiprecision;

int main(){
    cpp_dec_float_100 n(rsa100);

    auto log_n     = log(n);
    auto exp_log_n = exp(log_n);

    cout << std::fixed << log_n     << "\n";
    cout << std::fixed << exp_log_n << "\n";
}

查看 Live on Coliru