Remarks

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. The real part of the complex number is represented by x, and the imaginary part of the complex number is represented by y.

The Complex type uses the Cartesian coordinate system (real, imaginary) when instantiating and manipulating complex numbers. A complex number can be represented as a point in a two-dimensional coordinate system, which is known as the complex plane. The real part of the complex number is positioned on the x-axis (the horizontal axis), and the imaginary part is positioned on the y-axis (the vertical axis).

Any point in the complex plane can also be expressed based on its absolute value, by using the polar coordinate system., In polar coordinates, a point is characterized by two numbers:

Its magnitude, which is the distance of the point from the origin (that is, 0,0, or the point at which the x-axis and the y-axis intersect).

Its phase, which is the angle between the real axis and the line drawn from the origin to the point.

Instantiating a Complex Number

You can assign a value to a complex number in one of the following ways:

By passing two Double values to its constructor. The first value represents the real part of the complex number, and the second value represents its imaginary part. These values represent the position of the complex number in the two-dimensional Cartesian coordinate system.

By casting (in C#) or converting (in Visual Basic) a Decimal or BigInteger value to a Complex object. The value becomes the real part of the complex number, and its imaginary part equals 0.

By assigning the complex number that is returned by a method or operator to a Complex object. For example, Complex.Add is a static method that returns a complex number that is the sum of two complex numbers, and the Complex.Addition operator adds two complex numbers and returns the result.

The following example demonstrates each of these five ways of assigning a value to a complex number.

Methods to perform other numerical operations on complex numbers. In addition to the four basic arithmetic operations, you can raise a complex number to a specified power, find the square root of a complex number, and get the absolute value of a complex number.

Methods to perform trigonometric operations on complex numbers. For example, you can calculate the tangent of an angle represented by a complex number.

Note that, because the Real and Imaginary properties are read-only, you cannot modify the value of an existing Complex object. All methods that perform an operation on a Complex number, if their return value is of type Complex, return a new Complex number.

Precision and Complex Numbers

The real and imaginary parts of a complex number are represented by two double-precision floating-point values. This means that Complex values, like double-precision floating-point values, can lose precision as a result of numerical operations. This means that strict comparisons for equality of two Complex values may fail, even if the difference between the two values is due to a loss of precision. For more information, see Double.

For example, performing exponentiation on the logarithm of a number should return the original number. However, in some cases, the loss of precision of floating-point values can cause slight differences between the two values, as the following example illustrates.

In the following example, division by Zero produces a complex number whose real and imaginary parts are both Double.NaN. As a result, performing multiplication with this value also produces a complex number whose real and imaginary parts are Double.NaN. Similarly, performing a multiplication that overflows the range of the Double type produces a complex number whose real part is Double.NaN and whose imaginary part is Double.PositiveInfinity. Subsequently performing division with this complex number returns a complex number whose real part is Double.NaN and whose imaginary part is Double.PositiveInfinity.

Note that this applies to any intermediate calculations performed by a method. For example, the multiplication of new Complex(9e308, 9e308) and new Complex(2.5, 3.5) uses the formula (ac - bd) + (ad + bc)i. The calculation of the real component that results from the multiplication evaluates the expression 9e308 * 2.5 - 9e308 * 3.5. Each intermediate multiplication in this expression returns Double.PositiveInfinity, and the attempt to subtract Double.PositiveInfinity from Double.PositiveInfinity returns Double.NaN.

Formatting a Complex Number

By default, the string representation of a complex number takes the form (real,imaginary), where real and imaginary are the string representations of the Double values that form the complex number's real and imaginary components. Some overloads of the ToString method allow customization of the string representations of these Double values to reflect the formatting conventions of a particular culture or to appear in a particular format defined by a standard or custom numeric format string. (For more information, see Standard Numeric Format Strings and Custom Numeric Format Strings.)

One of the more common ways of expressing the string representation of a complex number takes the form a + bi, where a is the complex number's real component, and b is the complex number's imaginary component. In electrical engineering, a complex number is most commonly expressed as a + bj. You can return the string representation of a complex number in either of these two forms. To do this, define a custom format provider by implementing the ICustomFormatter and IFormatProvider interfaces, and then call the String.Format(IFormatProvider, String, Object[]) method.

The following example defines a ComplexFormatter class that represents a complex number as a string in the form of either a + bi or a + bj.

Converts the value of the current complex number to its equivalent string representation in Cartesian form by using the specified format and culture-specific format information for its real and imaginary parts.