1

I am trying to calculate the value of [e^i6(theta)]^2 using C. I am just showing few lines of my code. I have added additional header file as suggested by this answer (How to work with complex numbers in C?):

#include <stdio.h>
#include <math.h>

float theta; 
float dist,xcoord,division;

fprintf(fs,"%f\t%f\t%f\t%f\t%f\n",
    dist, xcoord, division,
    6*theta, pow(exp(I*6*theta),2));

My output is:

1.00000

first 3 lines of my output file:

 94.214905       68.130005       0.723134        4.574803        1.000000
 107.493179      -33.500000      -0.311648       11.326338       1.000000
 120.586807      52.529999       0.435620        6.720418        1.000000   

Which I don't think is correct. What could I possibly include to make it work? I agree I can break down this formula in cos and sin but I am looking for a direct option.

Community
  • 1
  • 1
Grayrigel
  • 3,474
  • 5
  • 14
  • 32

1 Answers1

0

You should use <complex.h> instead of <math.h>. I is defined in complex.h only. Should trigger a warning at compile time

Also, as mentioned, use complex functions:

printf("I = %f%+fi\n", crealf(I), cimagf(I));
z = clog(cexp(I * 6.0 * theta));
printf("z = %f%+fi\n", crealf(z), cimagf(z));

Output:

I = 0.000000+1.000000i
z = 0.000000-1.708382i

Don't forget math lib at link time: -lm

levif
  • 2,156
  • 16
  • 14