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!
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
You can find details here
A simple approach would be
I get this question recently as well and find a easy way for future reader:
Just use
<complex>
library like the followingAnother way is to use
std::literals::complex_literals::operator""i
after C++14:Output:
Here is a short complete example:
Tested with g++
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
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:
....
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.