我正在尝试重载运算符以分割两个复数
使用3 + 2i / 4 - 3i进行测试
complex g(3, 2);
complex f(4,-3);
cout << g / f << endl;
自从我们离开后,我添加了* -1.0
(4 * 4)+(3 * -3)i ^ 2在数学中是25
((3 * 4)+(3 * -3)* -1)是我的意图
测试我得到-0.545455 - 1.72727i
在我添加* 1.0之前,我得到了
0.24 +0.76i
非常接近
0.24 + 0.68i
答案
complex complex :: operator/ (complex& x) {
complex conjugate = x.conj();
double j = (real * conjugate.real) + (imag * conjugate.imag); // real
double u = (real * conjugate.imag) + (imag * conjugate.real); // imag
double h = (((conjugate.imag * imag)* -1.0) + (real * conjugate.real)) + ( (real*conjugate.imag) + (imag * conjugate.real));
return complex(j/h,u/h);
}
答案 0 :(得分:2)
这是错误的:
TryShowAsStandaloneAsync
应该是Window.Activate
,而不是double j = (real * conjugate.real) + (imag * conjugate.imag); // real
。
这是对的:
-
尽管+
和double u = (real * conjugate.imag) + (imag * conjugate.real); // imag
都被无意义地命名。
这里发生了什么?
j
分母只是u
,即:
double h = (((conjugate.imag * imag)* -1.0) + (real * conjugate.real)) + ( (real*conjugate.imag) + (imag * conjugate.real));
旁注,您希望通过引用const来获取您的参数。