views:

1576

answers:

6

How can I add very large numbers in C++?

A: 

You could use a library like LiDIA for a 'big integer' class.

Geoffrey Chetwood
A: 

You can find a big decimal implementation at http://speleotrove.com/decimal/

tvanfosson
A: 

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
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
You mean 64 bits, not 64 bytes.
TonJ
Accurate. sizeof(long) >= sizeof(int) is not the only requirement; the 32 bits range is another.
MSalters
+3  A: 

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
+1  A: 

Do a Google on "Bigint C++" This will provide you with a list of arbitrator precision integer arithmetic libraries.

Martin York
A: 

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