Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function – sin()

C Library Function - sin()

C Standard Library - <math.h> C Standard Library -

Description

C Library Function double sin(double x) Returns the cosine of the radian angle x The sine of.

sin() is a function in the C standard library a function in used to calculate the sine of a given angle (in radians). The sine function (sin) is a basic trigonometric function with wide applications in many mathematical, physical, and engineering fields.

Declaration

Below is the declaration of the sin() function.



#include 



double sin(double x);

float sinf(float x);

long double sinl(long double x);

Parameters

Return Value

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

Example

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

Example

#include

#include

#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's compile and run the above program, which will produce the following result:



45.000000 的正弦是 0.707107 度

Calculate the sine of multiple angles

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

Example

#include

#include

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 Analysis

Let's 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 needed. The function does not set errno, and will not return NaN unless the input itself is NaN or infinity. In that case, the returned result will be NaN or infinity.

Summary

sin() The function is used to calculate the sine of a given angle (in radians) and is an important tool for handling trigonometric operations. By properly using sin() function, you can obtain accurate results in mathematical computations, physics simulations, and engineering applications.

C Standard Library - <math.h> C Standard Library -