How to use complex number "i" in C++

Cancan picture Cancan · Jul 29, 2013 · Viewed 40k times · Source

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<complex> and #include<cmath>, and then they used the overloaded symbol I such as exp(2*I) . But it seems it doesn't work in my visual studio compiler. So, can anyone give a simple example of using complex exponential? Thanks!

Answer

Mike22LFC picture Mike22LFC · Sep 16, 2015

I get this question recently as well and find a easy way for future reader:

Just use <complex> library like the following

#include <iostream>
#include <complex>
using namespace std ;

int main(int argc, char* argv[])
{
    const   complex<double> i(0.0,1.0);    
    cout << i << endl ;

    return(0) ;
}