BigInteger: Wow!

An interesting new construct in .NET 4 is the BigInteger.

You may recall that the max value of the following integer types are:

Int16.MaxValue = 32,767

Int32.MaxValue = 2,147,483,647

Int64.MaxValue = 9,223,372,036,854,775,807

Now, it won’t be often that you will need a larger integer type than Int64, but if it happens that you do need one, BigInteger will fill the bill! And how large can a BigInteger get? Thinking to blow an exception with the operation, I cubed Int64.MaxValue and did not exceed it with this result:


Wow. I then looked it up and found out that:

The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds.

Apparently, you can make a BigInteger as arbitrarily large as you want, up until the point where you exceed the available memory of the machine and blow an OutOfMemoryException.

Interestingly, I found that the really long number I gave above is more than a octillion times as large as the number of inches to the farthest galaxy yet discovered (a mere 13 billion light-years away). Of course, if things continue as they appear to be going, we may yet need the BigInteger type to represent the size of the US national debt. Otherwise, I cannot imagine needing it for any purpose I expect to encounter.

But what fun!

Check out more about BigInteger HERE.


But with the huge possibilities of BigInteger come some performance hits!  Doing computations with it costs considerably more CPU time than even Int64. 

Int32 i = Int32.MaxValue;

for (Int32 y = 0; y < i; y++) { }

for (BigInteger b = 0; b < i; b++) { }

The first for/next loop takes 6.95 seconds on my machine, but the second takes 209.44 seconds!  Wow again!  Best use BigInteger only where absolutely necessary or where only a few calculations are needed.

This entry was posted in Miscellaneous. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s