C cos()

The cos() function defined in the math.h header file. It helps to return the cosine value of the given radian value.


double cos(double x); #where x can be in int, float or long double

Also, two functions cosf() and cosl() were used with type float and long double respectively.


float cosf(float x); 
long double cosl(long double x); 

cos() Parameters:

The cos() function takes a single parameter which is a value representing an angle expressed in radians.

Parameter Description Required / Optional
double value A double value indicates angle in radians Required

cos() Return Value

The return value of cos() function is a number between 1 and -1.

Input Return Value
-1<x ≥ 1 a number greater than or equal to 0
x < -1 NaN (not a number)

Examples of cos()

Example 1: Working of cos() function in C?


#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main () {
   double v, out, value;

   v = 60.0;
   value = PI / 180.0;
   out = cos( v*value );
   printf("Cosine value of %lf is %lf degrees\n", v, out);
   
   v = 90.0;
   value = PI / 180.0;
   out = cos( v*value );
   printf("Cosine value of %lf is %lf degrees\n", v, out);
   
   return(0);
}

Output:


Cosine value of 60.000000 is 0.500000 degrees
Cosine value of 90.000000 is 0.000000 degrees

Example 2: How cos() function works in C?


#include <stdio.h>
#include <math.h>
#define PI 3.14159265

int main()
{
   double pr = 30, output;

   // Converting to radian
   pr = (pr * PI) / 180;
   output = cos(pr);

   printf("The cos value of %.2lf radian = %.2lf", pr, output);

   return 0;
}

Output:


The cos value of 0.52 radian = 0.87