Edit: I made a mistake. The code after the horizontal line computes z2 / z3
, NOT (z2 + z3)/(z3 + z2)
. The following gets the correct result:
Complex z2(4, 4);
Complex z3(2, 2);
Complex numerator = z2.add(z3);
Complex denominator = z3.add(z2);
Complex conjugate = denominator.conjugate();
numerator = numerator.multiply(conjugate);
denominator = denominator.multiply(conjugate);
Complex result = numerator.multiply(denominator.inverse());
cout<<"(z2 + z3) / (z3 + z2) = "<< result.str(); // 1
Pulled the formula from Wikipedia.
Complex z2(4, 4);
Complex z3(2, 2);
// z2.real = A
// z2.imag = B
// z3.real = C
// z3.imag = D
// Formula is:
// a*c + b*d + b*c - a*d
// -----------------------
// c^2 + d^2 c^2 + d^2
double first = ((z2.real * z3.real) + (z2.imag * z3.imag))
/(z3.real * z3.real + z3.imag * z3.imag);
double second = ((z2.imag * z3.real) - (z2.real * z3.imag))
/(z3.real * z3.real + z3.imag * z3.imag);
Complex result(first, second);
cout<<"z2 / z3 = "<<result.str()<<endl;
// Result: 2