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

2020-05-29 01:35发布

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!

6条回答
欢心
2楼-- · 2020-05-29 02:09

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
}
查看更多
仙女界的扛把子
3楼-- · 2020-05-29 02:12

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) ;
}
查看更多
放荡不羁爱自由
4楼-- · 2020-05-29 02:15

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)
查看更多
Lonely孤独者°
5楼-- · 2020-05-29 02:18

Here is a short complete example:

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

using namespace std;
typedef complex<double> 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++

查看更多
Root(大扎)
6楼-- · 2020-05-29 02:26

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..

查看更多
唯我独甜
7楼-- · 2020-05-29 02:34

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.

查看更多
登录 后发表回答