Extend the complex number class to work with real numbers in C#

The example Make a complex number class that demonstrates 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 complex number (for example, 7 = 7 + 0i), it might also be nice to be able to combine doubles and Complex objects 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 cannot use the operators you'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 plus 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 uses the existing Complex-to-Complex + operator to add them. The other new operators work similarly. For example, the following code shows how the example 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 A + real but you cannot perform real + A. If you want to be able to add a Complex object to a real number, you need to define a whole new set of operators.

Fortunately this is again 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 to me so I prefer to make the cast explicit.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.