39

I'm doing a project which requires really big numbers, up to 100 digits. I have read that java supports big integers (java.Math.BigInteger), and I want to know if there is something like that in C++. So, here is my question: Is there a standard or non-standard c++ library which implements big integers?

Note: If there is no standard implementation for big integers, I would like a simple non-standard. Thanks in advance.

Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
  • 3
    You can answer this with a simple websearch. For example: https://mattmccutchen.net/bigint/ – David Heffernan Oct 20 '12 at 11:51
  • 1
    @DavidHeffernan: I have to say that I don't mind this as long as there isn't a duplicate SO question; SO questions get improved over time, and the best answers can rise to the top. Essentially this question is more about filtering the possibilities (what should I use) rather than finding them. It's not worded that way, and it is a slightly lazy question, but at least we'll have an answer for it on SO. – Phil H Oct 20 '12 at 12:09
  • This question is offtopic because it asks for a library. The accepted answer is only useful to those future visitors who don't mind placing their entire program under the GNU Public License. – Kaz Feb 16 '15 at 19:11
  • 2
    @Kaz The second sentence of your comment makes no sense, GMP is under **L**GPL. – Marc Glisse Feb 20 '16 at 18:58
  • My implementation here: http://www.codeproject.com/Articles/1117799/N-An-experimental-multithreaded-bignum-library-for – Michael Chourdakis Aug 12 '16 at 16:04
  • Also see [How to add 2 arbitrarily sized integers in C++?](https://stackoverflow.com/a/45940572/608639) It shows you how to use Botan, Crypto++ and OpenSSL big numbers. OpenSSL `BIGNUM` is even wrapped in C++ classes to provide operator overloading and resource management for you. – jww Dec 18 '17 at 05:18

4 Answers4

37

The GNU Multiple Precision Arithmetic Library does what you want http://gmplib.org/

Gnu MP is a C library but it has a C++ class Interface and if you are interested only in big integers, you may just deal with mpz_class. Look at the sample below which I took from the page C++ Interface General

 int main (void)
 {
   mpz_class a, b, c;

   a = 1234;
   b = "-5678";
   c = a+b;
   cout << "sum is " << c << "\n";
   cout << "absolute value is " << abs(c) << "\n";

   return 0;
 }
saeedn
  • 3,288
  • 1
  • 21
  • 20
  • 2
    I would like a simple implementation – Rontogiannis Aristofanis Oct 20 '12 at 12:01
  • 8
    @RondogiannisAristophanes bignum implementations are not simple – Stephan Dollberg Oct 20 '12 at 12:06
  • @RondogiannisAristophanes Simple implementation or simple interface? GMP's interface is not so simple, but it's easy to get used to. And it is powerful and efficient. – saeedn Oct 20 '12 at 12:34
  • 8
    Not thrilled. Not enough to downvote, but (1) The question is tagged C++. GMP is C. (2) A good C++ interface will be easy to use; e.g., addition is just '+'. (3) The question asked for big integers. GMP is bignums of all sort. – David Hammen Oct 20 '12 at 13:19
  • 6
    @DavidHammen Maybe I should have addressed a specific part of GMP, not the whole library. About (1) and (2), GMP v5 has a C++ class Interface http://gmplib.org/manual/C_002b_002b-Class-Interface.html which has also overloaded some operators. – saeedn Oct 20 '12 at 18:22
15

Unfortunately, there is no standard library for big numbers. You said you are looking for a "simple" library, the simplest library I know of is InfInt. It consists of just one header file. Its usage is fairly simple. Here is a sample code:

InfInt myint1 = "15432154865413186646848435184100510168404641560358";
InfInt myint2 = 156341300544608LL;

myint1 *= --myint2 - 3;
std::cout << myint1 << std::endl;
Tommy
  • 739
  • 8
  • 24
user2001885
  • 389
  • 3
  • 5
  • Any idea how fast is that compared to gmp ? – ElefEnt Mar 21 '15 at 03:25
  • Link is broken. Project "infint" has moved to another location on the Internet. – Muhamed Huseinbašić May 02 '16 at 07:18
  • 7
    @ElefEnt: I tried both of them, GMP was several orders of magnitude faster in my case. (63 minutes vs. 40 seconds for a certain task.) However, I used the highly optimized [MPIR](https://github.com/wbhart/mpir) port of GMP. – aedm Oct 30 '16 at 14:11
  • 2
    InfInt is incredibly slow! A couple of order of magnitude slower them GMP! – Davide Spataro Dec 13 '16 at 15:25
  • 2
    why did you repost from this https://stackoverflow.com/a/16801910/995714? – phuclv Apr 16 '18 at 07:54
  • Anyone looking for the library's working link: [it's in this comment](https://stackoverflow.com/questions/10576314/a-good-and-basic-implementation-of-bigint-class-in-c/16801910#comment74771458_16801910). – The Amateur Coder Jan 23 '22 at 17:37
6

You said you want a simple interface/implementation, here's one http://www.di-mgt.com.au/bigdigits.html. Personally I'd still go for GMP however.

john
  • 85,011
  • 4
  • 57
  • 81
-1

You will be taking input in a char array, and then will change it into an int array. The size of array can also be changed.

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int b, i, arrayint[100];
    char arraychar[100];

    for(i = 0; i < 100; i++)
        cin >> arraychar[i];

    for(i = 0; i < 100; i++)
        cout << arraychar[i];

    cout << endl;

    for(i = 0; i < 100; i++)
        arrayint[i] = arraychar[i] - '0';

    for(i = 0; i < 100; i++)
        cout << arrayint[i];

    return 0;
}
ilim
  • 4,477
  • 7
  • 27
  • 46
my time
  • 39
  • 6