Skip to content
Search

Std::complex

lastmod 2015-03-19created 2015-02-237 edits

The complex class is a template class. Use it when you need complex numbers and complex number operations.

Imaginary number

The imaginary unit i can be expressed as follows.

#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;
    return 0;
}

Favorite site