How to Use Complex Number "I" in C++

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!

7

6 Answers

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) ;
}

Another way is to use std::literals::complex_literals::operator""i after C++14:

#include <iostream>
#include <complex>

int main() {
    using namespace std::complex_literals;
    auto c = 1.0 + 3.0i;
    std::cout << "c = " << c << '\n';
}

Output:

c = (1,3)
0

Here is a short complete example:

#include <iostream>
#include <complex>
#include <cmath>

using namespace std;
typedef complex<double> dcomp;

int 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++

0

You can find details here

A simple approach would be

#include <complex>

using std::complex;
const double pi = 3.1415;
void foo()
{
    complex<double> val(polar(1, pi/2.0); Create a complex from its olar representation
}

The following code in C++ shows a macro for implementing the imaginary number j. It is well known that in programming the terms i and j are commonly used as counter variables. I instead use the capital letter J to represent the imaginary number to avoid any confusion.

/ * dcomplex.h

#ifndef DCOMPLEX_H_
#define DCOMPLEX_H_
#define J dcomplex(0.0,1.0)
typedef std::complex<double> dcomplex;
#endif /* DCOMPLEX_H_ */

Using this macro, the imaginary number J [together with the complex library] can be used in the main code. An example of its use is shown below:

....
....
#include <complex>
#include "dcomplex.h"

....
....
 tmp = tmp + t[n]*exp( (2.0*PI*(double)n*(double)l/(double)tab_size)*J );
....

....

where tmp, t[n] are variables of a complex type, and J is the imaginary number. The variables n, l, and tab_size are of an integer type. The constant PI is the well known constant 3.14... The function exp() is overloaded to handled complex numbers. [n.b. this code sample is part of a simple DFT]

Using this macro, the code is more readable..

pi, being an irrational number, cannot be exactly represented by a double. cos of an inexact approximation of pi is likely to yield a result which is close to but perhaps not exactly 1. Likewise sin of an inexact approximation of an inexact approximation of pi is like to result in a number is has a very small magnitude that is perhaps not exactly 0. Why not just define I to be std::complex(0.0, 1.0) and avoid the gratuitous inexactness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest