C library - csin() function



The C complex library csin() function operates the complex sine of a given complex number. This function defined under the header <complex.h>.

Syntax

Following is the C library syntax of the csin() function −

double complex csin(double complex z);

Parameters

This function accept only a single parameter(z) that define the complex number.

Return Value

The function return type is a double complex value. If no such error occur, it returns the complex sine of z.

Example 1

Following is the C library program that illustrates the complex sine of constant value using csin() function.

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

int main() {
   double complex z = 1 + 2 * I;
   double complex result = csin(z);

   printf("sin(1 + 2i) = %.3f + %.3fi\n", creal(result), cimag(result));
   return 0;
}

Output

On execution of above code, we get the following result −

sin(1 + 2i) = 3.166 + 1.960i

Example 2

Here, we customize our own function named custom_csin() which accepts angle value to determine the task of sine(user provided angle in radian).

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

double complex custom_csin(double angle) {
   // Implement your custom complex sine calculation here
   return csin(angle);
}

int main() {
   double angle = 0.5; 
   double complex res = custom_csin(angle);

   printf("Custom sin(%.3f) = %.3f + %.3fi\n", angle, creal(res), cimag(res));
   return 0;
}

Output

After executing the above code, we get the following result −

Custom sin(0.500) = 0.479 + 0.000i

Example 3

Below the example demonstrates the complex sine of imaginary value. Here, we observe the behavior of csin() when the input is purely imaginary.

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

int main() {
   double y_values[] = { 1.0, 2.0, 3.0 };
   for (int i = 0; i < 3; ++i) {
       double complex p = I * y_values[i];
       double complex res = csin(p);
       printf("sin(i%.1f) = %.3f + %.3fi\n", y_values[i], creal(res), cimag(res));
   }

   return 0;
}

Output

The above code produces the following result −

sin(i1.0) = 0.000 + 1.175i
sin(i2.0) = 0.000 + 3.627i
sin(i3.0) = 0.000 + 10.018i
c_library_complex_h.htm
Advertisements