Title: Make a complex number class that works with real numbers in C#
The example Make a complex number class with overloaded operators in C# builds a simple Complex class that includes overloaded +, -, *, and / operators that let you combine Complex objects. For example, a program can execute the following code.
Complex A = new Complex(1, 2);
Complex B = new Complex(3, 4);
Complex C = A + B;
Unfortunately those operators are only defined when you are combining two Complex objects. Because a real number is also a kind of complex number (for example, 7 = 7 + 0i), it would be nice to be able to combine a double and a Complex object as in:
Complex A = new Complex(1, 2);
double real = 17;
Complex C = A + real;
Unfortunately C# doesn't know that a real number is a type of complex number, so it can't use the operators we've defined so far to perform these operations. To do that, you need to create a new set of overloaded operators defined between the Complex class and the double data type. Fortunately that isn't too hard.
The following code shows the code that defines the + operator to add a Complex and a real number.
// Return A + real.
public static Complex operator +(Complex A, double real)
{
return A + new Complex(real);
}
Notice that the code does as little new work as possible. It converts the double into a Complex object and then uses the existing Complex-to-Complex + operator to add them. The other new operators work similarly. For example, the following code shows how the program defines / for a Complex and a double.
// Return A / real.
public static Complex operator /(Complex A, double real)
{
return A / new Complex(real);
}
So far so good, but there's another catch. Just because you have defined operators to combine Complex and real values doesn't mean you can combine real and Complex values. Now you can perform Complex + real, but you can't perform real + Complex. To do that you need to define a whole new set of operators.
Fortunately the current operators make that easy. The following code shows the new operators.
// Return real + A.
public static Complex operator +(double real, Complex A)
{
return A + real;
}
// Return real - A.
public static Complex operator -(double real, Complex A)
{
return -A + real;
}
// Return real * A.
public static Complex operator *(double real, Complex A)
{
return A * real;
}
// Return real / A.
public static Complex operator /(double real, Complex A)
{
return new Complex(real) / A;
}
The only one of these that is even a little tricky is /, which converts the real number into a Complex and then invokes the Complex-to-Complex version of /.
This example includes one final operator: a casting operator that can convert a double into a Complex.
// Double to Complex conversion.
// The explicit keyword means you must use an explicit cast.
public static explicit operator Complex(double real)
{
return new Complex(real);
}
This operator allows the code to perform casts from double to Complex as in the following code.
double real = 1337;
Complex A = (Complex)real;
If you use the keyword implicit instead of explicit in this operator, then the code could omit the cast and write:
double real = 1337;
Complex A = real;
That seems like it could cause confusion, so I'm making the cast explicit.
Download the example to experiment with it and to see additional details.
|