Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent sqrt(-1) in programming?

Tags:

c++

math

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?

like image 979
dato datuashvili Avatar asked Oct 19 '11 09:10

dato datuashvili


1 Answers

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));
like image 58
Flexo Avatar answered Sep 27 '22 21:09

Flexo