Skip to main content

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 say they are. BigInteger is for big integers and Complex is complicated ;)

BigInteger

BigInteger is a class, not a type (like float), which allows you to have an integer with no theoretical upper and lower limits! Why is that cool? think about Int64 which can do up to: 9,223,372,036,854,775,807. If you have an Int64 which has that massive value, and you add one to it, the Int64 it overflows and becomes -9,223,372,036,854,775,806. That is not possible with BigInt since it has no upper limit!

Being a class means it has methods and properties you can use too, for example some of the properties

  • IsZero: Tells you if it equals zero.
  • IsEven: Tells you if it is an even number.

An example of using it:

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

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

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

This gives the following output (look at the size of the number from the multiplication!):

image 

BigRational

What if you want to work with rational numbers with no limits, rather than integers? Then you can use the BigRational class the BCL team has made available at http://bcl.codeplex.com/

Complex

A complex number is a number that comprises a real number part and an imaginary number part. A complex number z is usually written in the form z = x + yi, where x and y are real numbers, and i is the imaginary unit that has the property i2 = -1.

That snippet is the first line from the documentation on System.Numeric.Complex and unfortunately I am not smart enough to know what they are talking about. So who should understand this?

  • Electrical engineers: Using Complex they can do the following: Resistance(R) and Reactance(X) to calculate the impedance Z.
  • Mathematicians: Vector Calculus as well as Graphs.
  • People using positional (mapping) info: X, Y coordinates on a  map or 2d plane.

For an example I will just wimp out and show you what the MSDN documentation has:

// Create a complex number by calling its class 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 return value of a method to a Complex variable.
Complex c4 = Complex.Pow(Complex.One, -1);
Console.WriteLine(c4);

// Assign the value returned by an operator to a Complex variable.
Complex c5 = Complex.One + Complex.One;
Console.WriteLine(c5);

// Instantiate a complex number from its polar coordinates.
Complex c6 = Complex.FromPolarCoordinates(10, .524);
Console.WriteLine(c6);

That produces:

image

Some info on complex is from: http://www.dotnetspider.com/resources/36681-Examples-On-Complex-Class-C-New-Feature.aspx