user466534
user466534

Reputation:

How to represent sqrt(-1) in programming?

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?

Upvotes: 6

Views: 4523

Answers (2)

I guess you're looking for #include <complex> e.g.:

std::complex<double> num(0,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;
}

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));

Upvotes: 19

Widor
Widor

Reputation: 13275

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

Upvotes: 1

Related Questions