Search code examples
ccomplex-numberseuler-angles

Why am I getting wrong imaginary number?


I want to collect polar coordinate values by converting them to Cartesian values in C.

But I'm getting wrong values at 12°:

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

int main()
{   
    int desire=1;
    int wind[desire];
    for (int i = 0; i < desire; i++)
    {
        printf("\nEnter degree of wind:");
        scanf("%d",&wind[i]);
    }
    
    double complex sum=0;

    for (int i = 0; i < desire; i++)
    {
        sum=cos(wind[i])+ sin(wind[i]) * I;
    }

    double convert(double radian); 

    printf("\n %.2f %.2fi",creal(sum),cimag(sum));
    double r =hypot(creal(sum),cimag(sum));
    double angle= convert(atanf(creal(sum)/cimag(sum))) ;

    printf("\n %.2f %.2f",r,angle);

    return 0;
} 

returning completely wrong sine(imaginary value)

0.84 -0.54i
1.00 -57.55

It should print0.97 +0.20i not 0.84 -0.54i.
What is wrong?

The convert function:

#include <math.h>
#include "convert.h"
double convert(double radian)
{
    return(radian * (180/M_PI));
}

Solution

  • Per 7.12.4.6 The sin functions (bolding mine):

    The sin functions compute the sine of x (measured in radians).

    cos() is similar.

    You're entering 12 radians.