-5

got an error One of the parameters of a binary operator must be the containing type

can not see where is wrong about the operator overloadin

error at public static Complex operator +(Complex c1, Complex c2)

namespace testComplex
{
    class Program
    {

        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
        }

        public static Complex operator /(Complex c1, Complex c2)
        {
            return new Complex(c1.Real / c2.Real, c1.Imaginary / c2.Imaginary);
        }

        public static Complex operator *(Complex c1, Complex c2)
        {
            return new Complex(c1.Real * c2.Real, c1.Imaginary * c2.Imaginary + c1.Real * c2.Imaginary + c2.Real * c1.Imaginary);
        }
        public static Complex operator -(Complex c1, Complex c2)
        {
            return new Complex(c1.Real - c2.Real, c1.Imaginary - c2.Imaginary);
        }

        //public static Complex Sqrt -(Complex c1, Complex c2)
        //{
            //return new Complex(c1.Real - c2.Real, c1.Imaginary - c2.Imaginary);
        //}
        static void Main(string[] args)
        {
            Complex a = new Complex(0.5,0);
            Complex b = new Complex(0.6,0);

            //Complex aa = Math.Exp(-(1 / 4) * a * (b / a + Math.Sqrt(-7 * Math.Pow(b, 2) / Math.Pow(a, 2))) / b + (1 / 4) * b * (a / b + Math.Sqrt(-7 * Math.Pow(a, 2) / Math.Pow(b, 2))) / a);
            //Jesus[i] = aa.Real;

            Complex aa = Complex.Exp(-(1 / 4) * a * (b / a + Complex.Sqrt(-7 * Complex.Pow(b, 2) / Complex.Pow(a, 2))) / b + (1 / 4) * b * (a / b + Complex.Sqrt(-7 * Complex.Pow(a, 2) / Complex.Pow(b, 2))) / a);
            Console.WriteLine(aa.Real);
            Console.WriteLine(aa.Imaginary);
            Console.ReadKey();
        }
    }
}
ControlPoly
  • 717
  • 2
  • 10
  • 21

1 Answers1

1

Here is the doc on complex structure in C#, there is a part explaining why you might be getting a NaN

You are doing divisions so you might be dividing by zero, you will need to debug your equation further, I would recommend checking it outside of C# with the values that give you a NaN

Simon Rapilly
  • 393
  • 1
  • 13
  • 1
    *missed your comment on the doc* Ahh.. and I did not see this answer before my last comment. It all makes sense now ;-) – Leigh Aug 10 '13 at 03:13