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.
Note that exponent of complex number equals:
e^(ix) = cos(x)+i*sin(x)
Then:
e^(-i) = cos(-1)+i*sin(-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With