使用 boost 库时不断出现太大错误 (cpp_int)
Getting constant too big error while using boost library (cpp_int)
因为我尝试使用大整数,所以我安装了 boost 库,但是当我尝试调试时,我不断收到太大的错误,而我认为 cpp_int 可以正确处理它吗?
你能看看我的代码和错误吗?
这是错误:
错误 C2177 常量太大 HW7 C:\Users\hmffa\source\repos\HW7\HW7.cpp 34
错误(主动)E0023 整数常量太大 HW7 C:\Users\hmffa\source\repos\HW7\HW7.cpp 34
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using namespace boost::multiprecision;
cpp_int Remainder(cpp_int base, cpp_int exp, cpp_int mod) {
cpp_int r = 1;
if (base < 0)
base += mod;
if ((base % mod) == 0)
return r = 0;
if (exp >= mod) {
base = base % mod;
exp = exp % (mod - 1);
}
while (exp > 0) {
if (exp % 2 == 1) {
r = (r * base) % mod;
}
exp = exp >> 1;
base = (base * base) % mod;
}
return r;
}
int main()
{
int B[3] = { 2,3,5 }, C[3] = { 0,0,0 };
cpp_int input = 2, pow = input, exp = 0, powerInput;
cpp_int p= 30903154482632612361920641803533;
int i = 0;
while (i < 3) {
exp++;
powerInput = Remainder(input, exp, p);
while (powerInput % B[0] == 0)
C[0]++;
while (powerInput % B[1] == 0)
C[1]++;
while (powerInput % B[2] == 0)
C[2]++;
for (int j = 0; j < 2; j++) {
if (C[j] != 0)
cout << C[j] << " ";
}
cout << endl;
if (C[0] != 0 || C[1] != 0 || C[2] != 0)
i++;
}
}
改变
cpp_int p= 30903154482632612361920641803533;
至
cpp_int p{"30903154482632612361920641803533"};
因为我尝试使用大整数,所以我安装了 boost 库,但是当我尝试调试时,我不断收到太大的错误,而我认为 cpp_int 可以正确处理它吗? 你能看看我的代码和错误吗? 这是错误:
错误 C2177 常量太大 HW7 C:\Users\hmffa\source\repos\HW7\HW7.cpp 34
错误(主动)E0023 整数常量太大 HW7 C:\Users\hmffa\source\repos\HW7\HW7.cpp 34
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using namespace boost::multiprecision;
cpp_int Remainder(cpp_int base, cpp_int exp, cpp_int mod) {
cpp_int r = 1;
if (base < 0)
base += mod;
if ((base % mod) == 0)
return r = 0;
if (exp >= mod) {
base = base % mod;
exp = exp % (mod - 1);
}
while (exp > 0) {
if (exp % 2 == 1) {
r = (r * base) % mod;
}
exp = exp >> 1;
base = (base * base) % mod;
}
return r;
}
int main()
{
int B[3] = { 2,3,5 }, C[3] = { 0,0,0 };
cpp_int input = 2, pow = input, exp = 0, powerInput;
cpp_int p= 30903154482632612361920641803533;
int i = 0;
while (i < 3) {
exp++;
powerInput = Remainder(input, exp, p);
while (powerInput % B[0] == 0)
C[0]++;
while (powerInput % B[1] == 0)
C[1]++;
while (powerInput % B[2] == 0)
C[2]++;
for (int j = 0; j < 2; j++) {
if (C[j] != 0)
cout << C[j] << " ";
}
cout << endl;
if (C[0] != 0 || C[1] != 0 || C[2] != 0)
i++;
}
}
改变
cpp_int p= 30903154482632612361920641803533;
至
cpp_int p{"30903154482632612361920641803533"};