How to represent sqrt (-1) in programming?

I want to introduce sqrt(-1) in C ++ because I am trying to implement the FFT algorithm. Is there a good way to introduce this?

+6
source share
2 answers

I think you are looking for #include <complex> for example:

 std::complex<double> num(0,1); 

In fact, you can use std::sqrt with this type of complex to calculate sqrt(-1) :

 #include <complex> #include <iostream> int main() { const std::complex<double> result = std::sqrt(std::complex<double>(-1,0)); std::cout << result << std::endl; } 

For wn=exp((2*pi*i)/n) you can do:

 const double pi = std::acos(-1.0); const std::complex<double> i(0,1); std::complex<double> wn = std::exp((2*pi*i)/double(n)); 
+17
source

I believe there is a Complex class that you should include: http://www.cplusplus.com/reference/std/complex/

+1
source

Source: https://habr.com/ru/post/899595/


All Articles