# C programming math.h cos, cosf and cosl

In C programming the <math.h> cos,cosf and cosl compute the cosine of the argument.The declaration of the functions are given below.

1 float cos(float x);
2 double cos(double x);
3 long double cos(long double x)
4 float cosf(float x);
5 long double cosl(long double x);

Parameters:
x -The value whose cosine is to be computed.

Return type
floating point value -The cosine of ‘x’.

All the functions are the same,they compute the cosine of ‘x’.The 1st,2nd and the 3rd function have the same name i.e. ‘cos’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

Some points to note:

i-The argument ‘x‘ is measured in radian.

ii-The default version called is the cos function with double as argument and return type(the second version).

Code example

double d=12 , x1=0 ;

float f=12 ;

long double ld=12;

printf(“%lf”, cos( d ) ) ; //calls the 2nd version
printf(“\n%f”, cos( f ) ) ; //calls the 1st version
printf(“\n%Lf”, cos( ld ) ) ; //calls the 3rd version

printf(“\n%lf”, cos(x1 ) ) ;
printf(“\n%lf”, cos(NAN) ) ;

Output in Visual Studio,

0.843854
0.843854
0.843854
1.000000
-nan(ind)

The output ‘-nan(ind)’ means it is not representable.

#### cosf

The ‘cosf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘cosf‘ stands for float and it signify the parameter and return type of the function.

Code example

float x= 100 ;

printf(“%f”, cos( x ) ); //Calls the 1st version
printf(“\n%f”, cosf( 100 ) ); //same as above

Output,

0.862319
0.862319

#### cosl

The ‘cosl‘ function is same as the 3rd function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘cosl‘ stands for long double and it signify the parameter and return type of the function.

Code example

long double x= 100 ;

printf(“%Lf”, cos( x ) ); //Calls the 3rd version
printf(“\n%Lf”, cosl( 100 ) ); //same as above

Output,

0.862319
0.862319

#### Making cos compute any value in degree

The argument passed to cos,cosf and cosl function is measured in radian.To make the cos function compute for degree type value,the degree should be first converted to radian type.Since cos accept only radian type conversion from degree to radian type is necessary.

The relation between radian and degree is shown below,

Multiplying the argument with (3.141592/180) and computing the cos of the value will give the cosine of the given degree.

Code example

double degree=45 ;

printf(“cos(45%c)=%lf”, (int)248 , cos((degree*3.141592) / 180));

Output,

cos(45°)=0.707107

The character of the integer value 248 is ‘°’-the degree sign.