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

C library function - cos()

C library function - cos()

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

Description

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

cos() is a function in the C standard library is a function used to compute the cosine of an angle (in radians). This function is very commonly used for trigonometric calculations in mathematical computations, physics simulations, and engineering applications.

Declaration

The following is the declaration of the cos() function.



#include 



double cos(double x);

float cosf(float x);

long double cosl(long double x);

Parameters

Return Value

Return x the cosine value, with a range of [-1, 1].

Example

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

Example

#include

#include

#define PI 3.14159265

int

main

(

)

{

   

double

x

,

ret

,

val

;

   x

=

60.0

;

   val

=

PI

/

180.0

;

   ret

=

cos

(

x

val

)

;

   

printf

(

"The cosine of %lf is %lf degrees.\n"

,

x

,

ret

)

;

   

   x

=

90.0

;

   val

=

PI

/

180.0

;

   ret

=

cos

(

x

val

)

;

   

printf

(

"The cosine of %lf is %lf degrees.\n"

,

x

,

ret

)

;

   

   

return

(

0

)

;

}

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



60.000000 的余弦是 0.500000 度

90.000000 的余弦是 0.000000 度

Computing the cosine of different angles

The following example shows how to compute the cosine 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

=

cos

(

angle

)

;

       

printf

(

"cos(%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:

cos(0) = 1.000000

cos(π/6) = 0.866025

cos(π/4) = 0.707107

cos(π/3) = 0.500000

cos(π/2) = 0.000000

cos(π) = -1.000000

Error handling

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

Summary

cos() The function computes the cosine of a given angle (in radians) and is an important tool for trigonometric operations. Through appropriate use of the cos() function, you can obtain accurate results in mathematical computations, physics simulations, and engineering applications.

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