Computing e ^ (- j) in C

I need to calculate the imaginary exponent in C.

As far as I know, C. does not have a complex number library. You can get e^x with exp(x) from math.h , but how can I calculate the value e^(-i) , where i = sqrt(-1) ?

+4
source share
7 answers

Note that the index of the complex number is equal to:

 e^(ix) = cos(x)+i*sin(x) 

Then:

 e^(-i) = cos(-1)+i*sin(-1) 
+7
source

There is a complex type in C99. Include complex.h ; you may need a link to -lm on gcc. Please note that Microsoft Visual C does not support complex ; if you need to use this compiler, perhaps you can sprinkle some C ++ and use the complex template.

I defined as an imaginary unit, and cexp is exponentiation. Full code example:

 #include <complex.h> #include <stdio.h> int main() { complex x = cexp(-I); printf("%lf + %lfi\n", creal(x), cimag(x)); return 0; } 

See man 7 complex more details.

+15
source

Using the Euler formula , you have e^-i == cos(1) - i*sin(1)

+6
source

e^-j is just cos(1) - j*sin(1) , so you can just generate the real and imaginary parts using real functions.

+2
source

Just use the cartesian form.

if z = m*e^j*(arg);

 re(z) = m * cos(arg); im(z) = m * sin(arg); 
+1
source

Does a C ++ function call a solution for you? C ++ STL has a nice complex class, and boost also offers some nice options. Write a function in C ++ and declare it "extern C"

 extern "C" void myexp(float*, float*); #include <complex> using std::complex; void myexp (float *real, float *img ) { complex<float> param(*real, *img); complex<float> result = exp (param); *real = result.real(); *img = result.imag(); } 

You can then call the function from any C code you rely on (Ansi-C, C99, ...).

 #include <stdio.h> void myexp(float*, float*); int main(){ float real = 0.0; float img = -1.0; myexp(&real, &img); printf ("e^-i = %f + i* %f\n", real, img); return 0; } 
+1
source

In C ++, this can be done directly: std :: exp (std :: complex (0, -1));

0
source

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


All Articles