Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing e^(-j) in C

I need to compute imaginary exponential in C.

As far as I know, there is no complex number library in C. It is possible to get e^x with exp(x) of math.h, but how can I compute the value of e^(-i), where i = sqrt(-1)?

like image 413
Erkan Haspulat Avatar asked Nov 28 '22 03:11

Erkan Haspulat


2 Answers

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

I is defined as the imaginary unit, and cexp does 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 for more information.

like image 101
Thomas Avatar answered Jan 27 '23 14:01

Thomas


Note that exponent of complex number equals:

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

Then:

e^(-i) = cos(-1)+i*sin(-1)
like image 41
Vladimir Avatar answered Jan 27 '23 16:01

Vladimir