.NET 4 Baby Steps: Part XII - Numbers

22032010170 Note: This post is part of a series and you can find the rest of the parts in the series index.

A new namespace has arrived in .NET 4 for those who spend a lot of time with numbers: System.Numerics, which has two classes—BigInteger and Complex—and they are exactly what they sound like. BigInteger is for big integers, and Complex is... complicated😉

BigInteger

BigInteger is a class (not a type, like float), allowing you to create an integer with no theoretical upper or lower limits! Why is that useful? Consider Int64, which maxes out at 9,223,372,036,854,775,807. If you assign this value to an Int64 and add 1, it overflows and becomes -9,223,372,036,854,775,806. But with BigInteger, this is impossible—it has no upper limit!

Being a class means it includes useful methods and properties, such as:

Here’s an example of its usage:

BigInteger firstBigInt = new BigInteger(Int64.MaxValue);
BigInteger secondBigInt = new BigInteger(Int64.MaxValue);

Console.WriteLine("First BigInteger is even? {0}", firstBigInt.IsEven);
Console.WriteLine("First BigInteger = 1? {0}", firstBigInt.IsOne);
Console.WriteLine("First BigInteger is a power of two? {0}", firstBigInt.IsPowerOfTwo);
Console.WriteLine("First BigInteger = 0? {0}", firstBigInt.IsZero);
Console.WriteLine("First BigInteger is positive (1), zero (0), or negative (-1)? {0}", firstBigInt.Sign);
Console.WriteLine("{0} multiplied by {0} is {1}", Int64.MaxValue, BigInteger.Multiply(firstBigInt, secondBigInt));

You can also use standard arithmetic operators (+, -, *, etc.) with it.

Here’s the output (notice the size of the result from multiplication!):

image

BigRational

If you need to work with rational numbers (fractions) without limits instead of integers, check out the BigRational class—available from the BCL team at http://bcl.codeplex.com/.

Complex

A complex number is a number consisting of a real part and an imaginary part. Typically written as z = x + yi, where x and y are real numbers, and i is the imaginary unit satisfying i² = −1.

(This definition is borrowed from the System.Numerics.Complex documentation.)

Who benefits from Complex numbers?

For a quick example, here’s some code from MSDN:

// Create a complex number using its constructor.
Complex c1 = new Complex(12, 6);
Console.WriteLine(c1);

// Assign a Double to a complex number.
Complex c2 = 3.14;
Console.WriteLine(c2);

// Cast a Decimal to a complex number.
Complex c3 = (Complex)12.3m;
Console.WriteLine(c3);

// Assign the result of a method to a Complex variable.
Complex c4 = Complex.Pow(Complex.One, -1);
Console.WriteLine(c4);

// Assign the result of an operation to a Complex variable.
Complex c5 = Complex.One + Complex.One;
Console.WriteLine(c5);

// Instantiate from polar coordinates.
Complex c6 = Complex.FromPolarCoordinates(10, 0.524);
Console.WriteLine(c6);

The output would look like this:

image

(Additional info from: http://www.dotnetspider.com/resources/36681-examples-on-complex-class-c-new-feature.aspx)