3

The largest data type for positive integers I know of is unsigned long long. Is there a way to work on integers as large as 2^9000000 in C++. I use gcc compiler in code-blocks but can also work on visual studio.

JohanC
  • 38,768
  • 7
  • 13
  • 34
Usama Tahir
  • 121
  • 6
  • It depends on what you want to do with the value. – Patrick Trentin Nov 05 '19 at 17:20
  • 1
    One option it to use a [big integer library](https://stackoverflow.com/q/12988099/10957435). –  Nov 05 '19 at 17:21
  • You can use a library like [GMP](https://gmplib.org/). Trying to do all that a library like that gives you, by hand, is likely to involve man-years of work. Just use an existing "arbitrary precision" library already. – Jesper Juhl Nov 05 '19 at 17:27
  • 1
    There is also [https://www.boost.org/doc/libs/1_71_0/libs/multiprecision/doc/html/index.html](https://www.boost.org/doc/libs/1_71_0/libs/multiprecision/doc/html/index.html) – drescherjm Nov 05 '19 at 17:27
  • ***Is there a way to work on integers as large as 2^9000000 in C++*** Nothing built into the language. Either use a library or write your own (academic exercise only). – drescherjm Nov 05 '19 at 17:30
  • 1
    @drescherjm That's also a fine option. And there are more: https://en.m.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software – Jesper Juhl Nov 05 '19 at 17:31
  • Very often when posed with a problem like this there is a trick you can use, Modular exponentiation is a classic, to avoid having to handle such large numbers. – user4581301 Nov 05 '19 at 20:02

1 Answers1

7

You need some kind of BigInt library. I personally prefer boost.multiprecision, as it contains most of the utilities an average code writer would need.

In terms of what you actually want to use, there's two obvious types to consider.

  • If you just need arbitrarily sized integers, boost::multiprecision::cpp_int is the way to go, which will store a number as big as 2^9000000 by allocating approximately 9 million bits of data (slightly more than one megabyte) and storing the entire number in there.
  • If you need big numbers, but don't need to preserve the precision of every last digit, you'll prefer something like the cpp_bin_float backend, although for that you'd probably have to define your own template, since the prebaked versions probably aren't big enough.

For the latter, a potential example:

using namespace boost::multiprecision;
using my_big_float = number<backends::cpp_bin_float<100, backends::digit_base_2, void, boost::int32_t, -9000000, 9000000>, et_off>;
//Defines the number to have 100 bits of precision, an exponent range from negative
//nine million to positive nine million, and uses 32-bit ints for the internal representation
//of the exponent. Allocator is void (because we don't need it) and et_off just turns off 
//expression templates, which we don't need

int main() {
    my_big_float fl = 5;
    fl = pow(fl, my_big_float{900007});
    std::cout << fl << std::endl;
}

7.88302e+629077

I don't know what your use-case is, but my guess is that the latter is going to be much better for your use-case than the former. You'll have to decide for yourself what the case is.

Xirema
  • 18,577
  • 4
  • 26
  • 60