开发者

How to use complex numbers in c++?

开发者 https://www.devze.com 2023-02-16 16:19 出处:网络
may you tell me how to write a complex variable in C++ code please? I do have separate real part psi_rl and imaginary part psi_im. Now I have to write psi = psi_rl + i psi_im. Do you have any idea ho

may you tell me how to write a complex variable in C++ code please?

I do have separate real part psi_rl and imaginary part psi_im. Now I have to write psi = psi_rl + i psi_im. Do you have any idea how to accomplish this 开发者_开发知识库task?

Thanks.


You should read the documentation for std::complex, it will give you answers to these questions and many more.


Something like this should give you the basic idea:

class complex
{
public:
    double real;
    double imag;
    complex(double real, double imag): real(real), imag(imag) {};
    complex operator+(complex c) { return complex(this->real+c.real, this->imag+c.imag); };
};

int main(int argc, char* argv[])
{
    complex a(1,2);
    complex b(-3,6);
    complex c = a+b;
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号