Search code examples
c++boostc++17

big ints with boost : too large to be represented in any integer type


I think i don't get something.

Is the class cpp_int from boost::multiprecision supposed to hold integers as big as one want ? Let's say i want to store the following ridiculously big integer. How am I supposed to do it ?

#include <boost/multiprecision/cpp_int.hpp>

using namespace boost::multiprecision;

cpp_int n = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;

The following code returns

error: integer literal is too large to be represented in any integer type


Solution

  • As detailed in the documentation, you need to construct with a string:

    cpp_int n{"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"};
    

    See https://www.boost.org/doc/libs/1_74_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html