Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Boost multiprecision cpp_int

Tags:

c++

boost

I try to get log of a big number. How should I do it? I cannot use gmp.hpp because it says Cannot open include file: 'gmp.h': No such file or directory

The following code

#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;
}

Give me error:

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 ==========
like image 926
Adrian Munteanu Avatar asked Oct 21 '22 14:10

Adrian Munteanu


1 Answers

log takes a real number, not integer.

#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";
}

See it Live on Coliru

like image 134
sehe Avatar answered Nov 12 '22 19:11

sehe