How do you deal with numbers larger than UInt64 (C#)
Asked Answered
F

8

23

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

Fcc answered 19/8, 2009 at 9:35 Comment(0)
S
22

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#.

Soapstone answered 19/8, 2009 at 9:39 Comment(1)
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.Soapstone
H
25

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.

Hagler answered 19/8, 2009 at 9:38 Comment(1)
If anyone has trouble with namespace issue, it's under System.Numerics;Fao
S
22

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#.

Soapstone answered 19/8, 2009 at 9:39 Comment(1)
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.Soapstone
M
14

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

Mallarme answered 19/8, 2009 at 11:2 Comment(3)
Amazing that nobody else mentioned this, given that OP didn't specify integers.Magically
Pete mentioned this.Dave
Sure your can use double to store large numeric values. But some calculations don't work on double the same as the other types. Check: https://mcmap.net/q/584250/-why-is-modulus-operator-not-working-for-double-in-cMilepost
D
6

BigInteger represents an arbitrarily large signed integer.

using System.Numerics;

var a = BigInteger.Parse("91389681247993671255432112000000");
var b = new BigInteger(1790322312);
var c = a * b;
Dysgenics answered 5/6, 2016 at 20:51 Comment(0)
C
2

Also, do check that you truly need a variable with greater capacity than Int64 and aren't falling foul of C#'s integer arithmetic.

For example, this code will yield an overflow error:

Int64 myAnswer = 20000*1024*1024;

At first glance that might seem to be because the result is too large for an Int64 variable, but actually it's because each of the numbers on the right side of the formula are implicitly typed as Int32 so the temporary memory space reserved for the result of the calculation will be Int32 size, and that's what causes the overflow.

The result will actually easily fit into an Int64, it just needs one of the values on the right to be cast to Int64:

Int64 myAnswer = (Int64)20000*1024*1024;

This is discussed in more detail in this answer.

(I appreciate this doesn't apply in the OP's case, but it was just this sort of issue that brought me here!)

Carlcarla answered 23/11, 2019 at 16:13 Comment(1)
Better yet Int64 myAnswer = 20000L * 1024L * 1024L;Euphemism
E
1

Decimal has greater range.

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

Eisenhart answered 19/8, 2009 at 9:38 Comment(0)
A
1

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.

Araroba answered 19/8, 2009 at 9:39 Comment(0)
S
0

You can use decimal. It is greater than Int64.

It has 28-29 significant digits.

Stalemate answered 19/8, 2009 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.