4

Possible Duplicate:
working with incredibly large numbers in .NET

I'm working on a simple project which has to use basic arithmetic functions such as +,-,*,/. The numbers that used by are too large to be stored in a 64 bit Integer.

I wondered if there is any way to store these numbers so i can use them to perform arithmetic functions.

Thank you!

Community
  • 1
  • 1
Koosbara
  • 45
  • 6

2 Answers2

11

The BigInteger (.NET 4.0+) structure to the rescue!

Represents an arbitrarily large signed integer.

If you are on a version pre-dating 4.0, check out the BigNumbers library.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Hi Thank you for your answer, but how can i change the value of that object? Say that i have a 30 digit number and i want to represent it as a BigInteger as you suggested, how should I put that value in the object? – Koosbara Feb 10 '12 at 21:10
  • 2
    @AmirSidis - Did you read the documentation and examples in the link? This is an immutable _structure_, just like `Int32` - you can't change the value of an instance. You can do arithmetic on it like you can on other integral types. – Oded Feb 10 '12 at 21:11
  • 1
    @AmirSidis - Look at the [`Parse`](http://msdn.microsoft.com/en-us/library/dd268271.aspx) methods. – Oded Feb 10 '12 at 21:12
  • Ohh my bad, I didn't see the Parse method. Thank you guys! – Koosbara Feb 10 '12 at 21:15
8

You can use the BigInteger type which was introduced in .NET 4.

Represents an arbitrarily large signed integer.


You can use a BigInteger instance as you would use any other integral type. BigInteger overloads the standard numeric operators to enable you to perform basic mathematical operations such as addition, subtraction, division, multiplication, subtraction, negation, and unary negation.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452