Complex Numbers
Complex numbers are a vital part of many mathematical calculations, yet Visual Studio does not natively support complex numbers, but fortunately, this is a very easy oversight to fix.
First, a little maths lesson. Imaginary numbers are numbers that do not exist, like for example the square root of -1. This number is denoted by i and is central to complex numbers. Complex numbers are defined as a number that is made up of an imaginary component as well as a real component, and is denoted by the formula x*i + y.
So some examples of complex numbers are 5i + 4, 2i + 0 (which simplifies to 2i), and 0i + 6 (which simplifies to simply 6).
What the above example shows as well, is that the range of complex numbers also includes all the real numbers.
The complex number class contains two variables to contain the real and imaginary components, namely, re and im. Most of the rest of the class are overloaded arithmetic operators.
The ^ operator takes a complex number and raises it to a specified power, returning a new complex number.
The + and - operators add and subtract two complex numbers, which entails adding or subtracting the real and imaginary parts of the one to the real and imaginary parts of the other complex number.
The * and / operators are a bit more complicated in how they are calculated, but the net result is the multiplication and division of the complex numbers.
The Abs() function returns the absolute value of the complex number, which is the square root of the sum of the squares of the real and imaginary components - very much like the absolute value of a normal vector.
Lastly, the Arg() function returns the arg of the complex number in degrees, which allows the complex number to be expressed in polar notation.
The full sourcecode for the MathLib library is available at https://github.com/sjmeunier/mathlib
Originally posted on my old blog, Smoky Cogs, on 23 Oct 2009
Updated 5 Oct 2016: Updated code snippet after refactoring MathLib library