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

前端 未结 5 502
闹比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: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 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 operator"" i(float d)
    {
        return std::complex{0.0L,static_cast( d )};
    }
    

    You could use this as follows:

    auto my_complex = 1i; // 0 + 1i
    

提交回复
热议问题