//Argument of a complex number public double Arg() { myDouble a = new myDouble(0.0); if (re > a) { return((im / re).atan()); } else if (re < a && im >= a) { return((im / re).atan() + Math.PI); } else if (re < a && im < a) { return((im / re).atan() - Math.PI); } else if (re == a && im > a) { return(Math.PI / 2); } else if (re == a && im < a) { return(-Math.PI / 2); } else { Console.WriteLine("An indeterminate value."); throw new System.Exception(); } }
// Division operator for Complex(divides op1 to op2 with the formula described below and returns as new Complex) // (a+b*i)/(c+d*i) = ((a+b*i)(c-d*i))/((c+d*i)(c-d*i)) = ((a*c + b*d)/(c*c +d*d)) + ((b*c-a*d)/(c*c+d*d)i) public static Complex operator /(Complex op1, Complex op2) { myDouble a = new myDouble(0.0); if (op2.re == a && op2.im == a) { Console.WriteLine("You can't divide by zero."); throw new System.DivideByZeroException(); } else { return(new Complex((op1.re * op2.re + op1.im * op2.im) / (op2.re * op2.re + op2.im * op2.im), (op1.im * op2.re - op1.re * op2.im) / (op2.re * op2.re + op2.im * op2.im))); } }
//Prints the complex number in the form of a+bi public void printFull() { myDouble a = new myDouble(0.0); if (im < a) { Console.WriteLine("{0:R}+{1:R}i", re.num, -im.num, "\n"); } else if (im == a) { Console.WriteLine("{0:R}", re.num); } else { Console.WriteLine("{0:R}-{1:R}i", re.num, im.num, "\n"); } }
static void Main(string[] args) { myDouble a = new myDouble(2.5); myDouble b = new myDouble(-5.5); myDouble c = new myDouble(-6.0); myDouble d = new myDouble(3.0); Complex example1 = new Complex(a, b); Complex example2 = new Complex(c, d); (example1 + example2).printFull(); (example1 + example2).printParts(); (example1 - example2).printFull(); (example1 - example2).printParts(); (example1 * example2).printFull(); (example1 * example2).printParts(); (example1 / example2).printFull(); (example1 / example2).printParts(); Console.WriteLine("{0:R}", example1.Abs(), "\n"); Console.WriteLine("{0:R}", example1.Arg(), "\n"); Console.WriteLine("{0:R}", example2.Abs(), "\n"); Console.WriteLine("{0:R}", example2.Arg(), "\n"); }
public Complex(myDouble _re, myDouble _im) { re = _re; im = _im; }