views:

393

answers:

6

In C#, how can one store and calculate with numbers that significantly exceed UInt64's max value (18,446,744,073,709,551,615)?

+1  A: 

Decimal has greater range.

There is support for bigInteger in .NET 4.0 but that is still not out of beta.

Josip Medved
+4  A: 

Can you use the .NET 4.0 beta? If so, you can use BigInteger.

Otherwise, if you're sticking within 28 digits, you can use decimal - but be aware that obviously that's going to perform decimal arithmetic, so you may need to round at various places to compensate.

Jon Skeet
+6  A: 

By using a BigInteger class; there's one in the the J# libraries (definitely accessible from C#), another in F# (need to test this one), and there are freestanding implementations such as this one in pure C#.

Steve Gilham
Also worth noting -- there's yet another implementation in the DLR to support Python and Ruby indefinite precision integers, but I haven't looked to see what the public API to that class is.
Steve Gilham
+1  A: 

There are several libraries for computing with big integers, most of the cryptography libraries also offer a class for that. See this for a free library.

Filip Navara
A: 

You can use decimal it's greater then int64

28-29 significant digits

Wael Dalloul
A: 

What is it that you wish to use these numbers for? If you are doing calculations with really big numbers, do you still need the accuracy down to the last digit? If not, you should consider using floating point values instead. They can be huge, the max value for the double type is 1.79769313486231570E+308, (in case you are not used to scientific notation it means 1.79769313486231570 multiplied by 10000000...0000 - 308 zeros).

That should be large enough for most applications

Pete