C++ complex numbers, what is the right format?

前端 未结 5 423
闹比i
闹比i 2020-12-28 08:59

I want to use C++ with complex numbers. Therefore I included #include . Now my question is: How do I declare a variable?(so what is the format ca

相关标签:
5条回答
  • 2020-12-28 09:23

    Here is an example of how to use . It compiles and runs under QT

    #include <QCoreApplication>
    #include<complex>
    #include<iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    std::complex<double> x=3.0-3.0i;
    std::complex<double> y=2.0+4.0i;
    
    cout.precision(3);
    cout<<"x="<<x<<" y="<<y<<'\n';
    cout<<" OR x real="<<real(x)<<" x imagine="<<imag(x)<<"\n\n";
    
    complex<double> sum = x + y;
    cout<<"The sum: x + y = "<<sum<<'\n';
    
    complex<double> difference = x - y;
    cout<<"The difference: x - y = "<<difference<<'\n';
    
    complex<double> product = x * y;
    cout<<"The product: XY = "<<product<<'\n';
    
    complex<double> quotient = x / y;
    cout<<"The quotient: x / y = "<<quotient<<'\n';
    
    complex<double> conjugate = conj(x);
    cout<<"The conjugate of x = "<<conjugate<<'\n';
    
    complex<double> reciprocal = 1.0/x;
    cout<<"The reciprocal of x = "<<reciprocal<<'\n';
    
    complex<double> exponential =exp(x);
    cout<<"The exponential  of x = "<<exponential<<'\n';
    
    double magnitude=2.0,phase=45;
    cout<<"magintude = "<<magnitude<<" phase = "<< phase<<" degrees\n";
    complex<double> pol= std::polar(2.0,(M_PI/180.0)*phase);
    cout<<"The polar: x , y = "<<pol<<'\n';
    
    return a.exec();
    }
    
    0 讨论(0)
  • 2020-12-28 09:25

    You define a variable by specifying a template parameter and specifying a name for the variable, about like with most other templates:

    std::complex<double> x(1, 1);
    

    The first parameter to the ctor is the real part, the second the imaginary part.

    Starting with C++ 14, a user-defined literal operator has been added, so you can initialize a complex variable with a somewhat more natural notation:

    using namespace std::literals;
    
    std::complex<double> c = 1.2 + 3.4i;
    

    In this case, (obviously enough) the 1.2 is the real part and the 3.4 is the imaginary part.

    0 讨论(0)
  • 2020-12-28 09:34

    Try this:

    #include <complex>
    #include <iostream>
    using namespace std;
    int main()
    {
        complex<double> a = {1,2};
        complex<double> b(3,4);
    
        cout << a + b << "\n";
    }
    
    0 讨论(0)
  • 2020-12-28 09:40
    // 1 + 2i
    std::complex<double> c(1, 2);
    
    0 讨论(0)
  • 2020-12-28 09:41

    The constructor of std::complex has two parameters:

    • The first, wich has the real part of the number.
    • The second, wich has the imaginary part of the number.

    For example:

    std::complex<float> my_complex(1,1); //1 + 1i 
    

    Also, C++11 introduces user defined literals, wich allows us to implement (Or be implemented by the standard library, as in this C++14 accepted proposal) a literal for easy-to-use complex numbers:

    constexpr std::complex<float> operator"" i(float d)
    {
        return std::complex<float>{0.0L,static_cast<float>( d )};
    }
    

    You could use this as follows:

    auto my_complex = 1i; // 0 + 1i
    
    0 讨论(0)
提交回复
热议问题