How to use complex number “i” in C++

后端 未结 6 840
暗喜
暗喜 2021-02-06 08:19

I am coding a simple DFT algorithm now and I want to use the complex number i in complex exponential. I saw somebody use #include and #include&

6条回答
  •  梦如初夏
    2021-02-06 09:12

    Here is a short complete example:

    #include 
    #include 
    #include 
    
    using namespace std;
    typedef complex dcomp;
    
    main() {
      dcomp i;
      dcomp a;
      double pi;
      pi = 2 * asin(1);
      i = -1;
      i = sqrt(i);
      a = exp(2*pi*i);
      cout << "i is " << i << "and Euler was right: e(i pi) = " << a << endl;
    } 
    

    Tested with g++

提交回复
热议问题