4

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

InfiniteLooper
  • 324
  • 2
  • 12
  • 2
    C++ itself only supports integer literals of a certain size. Most likely that's where the error is coming from. – NathanOliver Nov 11 '20 at 20:32
  • `99999999999...999` before it gets assigned to the mp::cpp_int is an integer and has already overflowed. – Mike Vine Nov 11 '20 at 20:32
  • 6
    `cpp_int` has a constructor that takes a string as its argument, although I'm trying to find a documentation page with more information than just [this one](https://www.boost.org/doc/libs/1_74_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html). ETA: [This page](https://www.boost.org/doc/libs/1_56_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html) gives some examples of using string ctors. – Nathan Pierson Nov 11 '20 at 20:33
  • 1
    Note that in practice it is unlikely that you will have to initialize an object with such a giant magic number. – François Andrieux Nov 11 '20 at 20:33
  • 1
    try `cpp_int n("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999");` – Mike Vine Nov 11 '20 at 20:34
  • Thanks, it was actually one of my troubles, finding other documentation for boost libs, better than the only one i found and the one you linked to. – InfiniteLooper Nov 11 '20 at 20:34
  • Thanks, I'm trying some arithmetics to play around some RSA keys. Your solution worked also with hex numbers. This magic number is not meant to be exaclty this one. – InfiniteLooper Nov 11 '20 at 20:40
  • @NathanOliver, thanks actually it was in the errow thrown : `integer litteral is too large`. Thanks for pointing this out. – InfiniteLooper Nov 11 '20 at 20:41
  • @InfiniteLooper -- Most, if not all big number libraries must have a string constructor or similar. The C++ standard integer types just cannot represent so many digits. – PaulMcKenzie Nov 11 '20 at 20:47
  • Does this answer your question? [Getting constant too big error while using boost library (cpp\_int)](https://stackoverflow.com/questions/61840403/getting-constant-too-big-error-while-using-boost-library-cpp-int) – phuclv May 05 '22 at 03:12

1 Answers1

6

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

ypnos
  • 50,202
  • 14
  • 95
  • 141