boost::multiprecision::cpp_int acting like unsigned long long

1.4k views Asked by At

So I have a programming assignment in which I have to work with 64 digit numbers. I am currently using the boost::multiprecision::cpp_int library but I'm not able to use it on large numbers.

For example-

#include <boost/multiprecision/cpp_int.hpp>

int main()
{
    boost::multiprecision::cpp_int n1 =123341257612045876129038576124390847381295732;   
}

This is giving the error that the code is too long. After experimentation, it seems like cpp_int (supposed to contain as large a number as you want) is acting like an unsigned long long (shown in the picture above), how do I solve this?

1

There are 1 answers

0
sehe On BEST ANSWER

Use the string constructor, because you cannot express the initializer in C++, otherwise.

Live On Coliru

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

int main() {
    boost::multiprecision::cpp_int n1("123341257612045876129038576124390847381295732");
    n1 *= n1;
    n1 *= n1;

    std::cout << n1 << "\n";
}

Prints

231437371927256216552064578371685752056581253909626001052591740261122589692668963795268703088073326299461305156397520102373182511147316463882992573577585984095769469664077598976

Which is ~2.31 × 10^176