Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++17

boost

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

like image 846
InfiniteLooper Avatar asked Sep 05 '25 03:09

InfiniteLooper


1 Answers

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

like image 60
ypnos Avatar answered Sep 07 '25 20:09

ypnos