C library functions -sin()

C standard library - <math.h>

Description

C Library Functionsdouble sin(double x)Returns the radian anglexthe sine of.

sin()is the C standard library<math.h>A function that calculates the sine value of a given angle (in radians). The sine function (sin) is a fundamental trigonometric function, widely used in many mathematical, physical, and engineering applications.

Declaration

Below is the declaration of the sin() function.

#include <math.h>

double sin(double x);
float sinf(float x);
long double sinl(long double x);

Parameter

  • x: the angle in radians, the type can bedouble、floatorlong double。

Return Value

This function returns the sine of x, in the range [-1, 1].

Example

The following example demonstrates the usage of the sin() function.

Example

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

#define PI 3.14159265

int main ()
{
   double x, ret, val;

   x = 45.0;
   val = PI / 180;
   ret = sin(x*val);
   printf("The sine of %lf is %lf degrees", x, ret);
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

45.000000 的正弦是 0.707107 度

Calculate the sine values of multiple angles

The following example demonstrates how to calculate the sine values of multiple angles:

Example

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

int main() {
    double angles[] = {0, M_PI / 6, M_PI / 4, M_PI / 3, M_PI / 2, M_PI};
    const char* angle_labels[] = {"0", "π/6", "π/4", "π/3", "π/2", "π"};
    int num_angles = sizeof(angles) / sizeof(angles[0]);

    for (int i = 0; i < num_angles; i++) {
        double angle = angles[i];
        double result = sin(angle);
        printf("sin(%s) = %f\n", angle_labels[i], result);
    }

    return 0;
}

Code explanation

  • Define an array containing multiple anglesangles, each angle expressed in radians.
  • Define a corresponding label arrayangle_labels, used for printed output.
  • UsageforLoop through each angle, calculate the sine value, and print the result.

Let us compile and run the above program, which will produce the following result:

sin(0) = 0.000000
sin(π/6) = 0.500000
sin(π/4) = 0.707107
sin(π/3) = 0.866025
sin(π/2) = 1.000000
sin(π) = 0.000000

Error Handling

sin()The function is valid for all real number inputs, so no special error handling is required. The function does not seterrno, nor will it return NaN unless the input itself is NaN or infinity. In this case, the returned result will be NaN or infinity.

Summary

sin()The function is used to calculate the sine value of a given angle (in radians) and is an important tool for handling trigonometric operations. Through reasonable usesin()The function can produce accurate results in mathematical calculations, physical simulations, and engineering applications.

C standard library - <math.h>

other extensions