如何在编程中表示sqrt(-1)? [英] How to represent sqrt(-1) in programming?
本文介绍了如何在编程中表示sqrt(-1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在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));
这篇关于如何在编程中表示sqrt(-1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文