Library Function cos in Math Class
Library Function cos of math.h Header File in C
On this page we will discuss about library function cos in math class which is used in C.The C header file math.h contains the standard math library functions that can be used for performing various mathematical operations. The acos function returns the cosine of x in radians.
Library Function cos in Math class used in C
In C programming language the cos function is included in math.h header file.
The range of input argument which is passed to cos function is not limited here as it can be any value either negative or positive and returns the cosine of parameter x in radians.
Note:
Mathematically we can define this function as:
cosx = cos(x)
cosx = cos(x)
Declaration of cos function
double cos(double x)
We can find the cosine of different data types like float or long double by using the specific function cosf and cosl respectively.
float cosf(float x); long double cosl(long double x);
Parameters of cos function
The x parameter is the angle in radians. The function returns a value of type double, which is the cosine of the angle.
Return value of cos function
The cos function returns the principal value of cosine of parameter x that has a value between -1 and 1, inclusive and it’s return type is double.
Parameter | Return Value |
---|---|
angle x | This function returns a value that lies in the interval [-1,1]. |
Implementation of Library Function math.h cos
Example 1:
Run
#include<stdio.h> #include<conio.h> #define PI 3.141592654 int main() { // Declaring temporary variables double angle = 4.12345, result; // Converting degrees to radian angle = (angle * PI) / 180; result = cos(angle); // Display the result of calculation printf("cos of %.2lf radian = %.2lf", angle, result); return 0; }
Output:
cos of 0.07 radian = 1.00
Example 2:
Run
#include<stdio.h> #include<conio.h> int main() { //positive number in radians printf("The cosine of %lf is %lf \n", 6.5, cos(6.5)); // negative number in radians printf("The cosine of %lf is %lf \n", -6.5, cos(-6.5)); //converting the degrees angle into radians and then applying cos() // degrees = 75.0 // constant PI = 3.14159265 double answer=cos(75.0 * (3.14159265 / 180.0)); // Displaying the result of calculation printf("The cosine of %lf is %lf \n", 75.0, answer); }
Output:
The cosine of 6.500000 is 0.976588 The cosine of -6.500000 is 0.976588 The cosine of 75.000000 is 0.258819
Note:
The parameter of cos function is an angle which is typically specified in radians having a floating point value, hence the format specifier used here for cos function is %lf.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment