How can I add very large numbers in C++?
Asked
Active
Viewed 5,104 times
3
-
This question is repeated. http://stackoverflow.com/questions/269268/how-to-implement-big-int-in-c – jaircazarin-old-account Dec 04 '08 at 05:24
-
Numbers are not necessarily ints so it may not be exactly the same. – tvanfosson Dec 04 '08 at 05:26
-
Also Check out here: http://stackoverflow.com/search?q=BigInt+c%2B%2B – Martin York Dec 04 '08 at 08:28
-
If you want to perform the multi-precision math yourself, then I suggest you take a look at Donald Knuth's [Art of Computer Programming](https://en.wikipedia.org/wiki/The_Art_of_Computer_Programming). I believe Volume II, Seminumerical Algorithms, Chapter 4, Multiple Precision Arithmetic, is what you are interested in. Also see [How to add 2 arbitrarily sized integers in C++?](https://stackoverflow.com/q/2926219/608639), which provides code for some C++ libraries and OpenSSL. – jww Aug 29 '17 at 17:52
6 Answers
3
consider a "bignum" library like: http://gmplib.org/ or http://ttmath.slimaczek.pl/ttmath. take a look at a simple bignum class: http://www.circlemud.org/~jelson/560/

Ray Tayek
- 9,841
- 8
- 50
- 90
1
Do a Google on "Bigint C++" This will provide you with a list of arbitrator precision integer arithmetic libraries.

Martin York
- 257,169
- 86
- 333
- 562
0
You can find a big decimal implementation at http://speleotrove.com/decimal/

tvanfosson
- 524,688
- 99
- 697
- 795
0
How big is "very large"? A signed long int can go up to 2,147,483,647 and an unsigned long int can go up to 4,294,967,295.

Patrick Harrington
- 47,416
- 5
- 23
- 20
-
Not accurate sizeof(long) >= sizeof(int). So technically long only has to be as big as int (and on most systems nowadays is just 32). long long (A C extension not yet officially supported by C++) has a minimum of 64 bytes. – Martin York Dec 04 '08 at 05:48
-
-
Accurate. sizeof(long) >= sizeof(int) is not the only requirement; the 32 bits range is another. – MSalters Dec 04 '08 at 09:47
0
GMP has a GMPXX C++ wrapper which is kind of nice. GMP supports both integer and floating point numbers, and is LGPL'ed.
I've used it. It's ok, but watch out for creating lots of temporaries. (Potential efficiency hit.)

Mr.Ree
- 8,320
- 27
- 30