我想在C ++中表示 sqrt(-1),因为我正在尝试实现FFT算法。是否有很好的方法来表示这个?
I want to represent sqrt(-1) in C++, because I am trying to implement an FFT algorithm. Is there a good way of representing this?
推荐答案我想你正在寻找 #include< ; complex> 例如:
std::complex<double> num(0,1);你可以使用 std :: sqrt 与此复合类型计算 sqrt(-1):
You can actually use std::sqrt with this complex type to compute 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; }对于 wn = exp i)/ n)你可以这样做:
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));