复数除法类

Complex Number Division class

本文关键字:除法      更新时间:2023-10-16

我正试图重载运算符来划分两个复数

使用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);
}

这是错误的:

double j = (real * conjugate.real) + (imag * conjugate.imag); // real

应该是-,而不是+

这是正确的:

double u = (real * conjugate.imag) + (imag * conjugate.real); // imag

虽然CCD_ 5和CCD_。

这是怎么回事?

double h = (((conjugate.imag * imag)* -1.0) + (real * conjugate.real)) + ( (real*conjugate.imag) + (imag * conjugate.real));

分母只是x*conjugate,它是:

double denom = x.real * x.real  + x.imag * x.imag;

附带说明,您希望通过引用const来获取您的论点。