The following code in C ++ shows a macro for implementing an imaginary number j. It is well known that in programming the terms i and j are usually used as countable variables. Instead, I use the capital letter J to represent the imaginary number, to avoid confusion.
/ * dcomplex.h
#ifndef DCOMPLEX_H_ #define DCOMPLEX_H_ #define J dcomplex(0.0,1.0) typedef std::complex<double> dcomplex; #endif
Using this macro, the imaginary number J [together with a 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, J is an imaginary number. The variables n, l, and tab_size are of integer type. The constant PI is a well-known constant 3.14 ... The function exp () is overloaded into processed complex numbers. [Notabene, this sample code is part of a simple DFT]
Using this macro, the code is more readable.
source share