Introducing: System.Numeric.BigInteger [Inbar Gazit]

Update: BigInteger was cut from the .NET 3.5 release.

Have you ever needed a really huge number?  So big it just couldn't fit into any other base-type in the framework?  Well, no problem anymore, just use the new BigInteger type. You can now have the ability to use integers of any arbitrary size, so they can exponentiate until the cows come home... or memory runs out, whichever comes first :-)

So, what's so "Big" about BigInteger?

I saw some people wondering whether we should have called this new type UnboundedInteger instead of BigInteger and I would like to tell you what I think. First, you should know that the BCL team does not take naming lightly. We spend a lot of time and energy making sure we choose the best names for our types, methods, properties etc. I believe that good names are not only technically accurate but also intuitive and easy to remember and use. That's one of the reasons that made us decide to use the name BigInteger. The other reason was that other languages are using the prefix "Big" for unbounded types.  Isn't that huge? Enormous? Gigantic? Well, I think it's simply Big...

Using the new BigInteger

So, what do you need to do in order to use this new type?

First you need to get the latest CTP for the new version of Visual Studio (code name – "Orcas") and install it so that you also get the new version of the .NET Framework including this new type. It would be a good idea to install it on a separate computer or make sure you back-up your important work first. Remember, this is pre-beta software... (in the January CTP you can choose either a Virtual PC image or to use the actual installation bits)

If you do install the bits, all you need is the new System.Core.dll as a reference in your project.  The new BigInteger class is in this new assembly.

From this point on I'm going to be talking C# although this can all work for other languages (such as VB.NET).

Create a new C# project and add a new Using System.Numeric at the top of your cs module. Now, you're ready to use BigInteger in your code.

Here is a sample code to calculate factorials:

using System;

using System.Numeric;

 

namespace TryOutBigInteger {

    class MyBigIntApp {

        static BigInteger BigFactorial(BigInteger x) {

            if (x < 0)

                return BigFactorial(-x);

            else if (x <= 1)

                return x;

            else return x * BigFactorial(--x);

        }

 

        static void Main(string[] args) {

            BigInteger x = BigInteger.Parse(Console.ReadLine());

            Console.WriteLine(BigFactorial(x).ToString());

            Console.ReadLine();

        }

    }

}

Here is what we get when we run it:

5

120

And:

300

306057512216440636035370461297268629388588804173576999416776741259476533176716867465515291422477573349939147888701726368864263907759003154226842927906974559841225476930271954604008012215776252176854255965356903506788725264321896264299365204576448830388909753943489625436053225980776521270822437639449120128678675368305712293681943649956460498166450227716500185176546469340112226034729724066333258583506870150169794168850353752137554910289126407157154830282284937952636580145235233156936482233436799254594095276820608062232812387383880817049600000000000000000000000000000000000000000000000000000000000000000000000000

Wow!  I wish that was my bank account balance :-)

BigInteger supports most of the operations that int supports including +, -, *, /, ++, --, %, DivRem GreatestCommonDivisor, Pow, ModPow, Abs and Negate.  All of these are static methods you can find using intellisense by typing "BigInteger".

Bitwise operations are not supported, but we will have an MSDN article coming soon with how to write your own library extension of BigInteger.

Please try the new BigInteger type and let us know what you think. We'd love to hear your feedback!