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)
?
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.