4

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

0

7 Answers 7

18

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.

1
  • I needed to do this manually.. Thanks for the future reference, I'll keep this in mind. May 14, 2010 at 19:31
8

Note that exponent of complex number equals:

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

Then:

e^(-i) = cos(-1)+i*sin(-1)
0
6

Using the Euler's Formula you have that e^-i == cos(1) - i*sin(1)

2
  • e^(-k) = cos(k) - i*sin(k), you mean.
    – Deep-B
    May 14, 2010 at 15:42
  • yes, but in his case k = 1. In any case the general form is e^(-ki) = cos(k)-i*sin(k), not e^(-k)
    – Jack
    May 14, 2010 at 16:42
2

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

1

Just use the cartesian form

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

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

Is calling a c++ function a solution for you? The C++ STL has a nice complex-class and boost also has to offer 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();
}

Then you can call the function from whatever 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
  • Thanks for the explanation but C++ is no good for me with this May 14, 2010 at 19:32
0

In C++ it can be done directly:

std::exp(std::complex<double>(0, -1));
1
  • this question is tagged with C, not C++
    – phuclv
    Aug 12, 2021 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.