C Library – math.h
Last Updated :
12 Sep, 2023
The <math.h> header file in C contains the standard math library functions which can be utilized for various mathematical operations. All math.h library functions accept double type as an argument and return double as a result. It also contains some commonly used constants defined as macros.
Syntax:
#include <math.h>
After including the header file, we can use its services in the program. There are two types of elements in math.h header file:
math.h Library Functions
These are functions that are defined inside the <math.h> header file that performs a particular mathematical operation when called.
Example:
C
#include <math.h>
#include <stdio.h>
int main()
{
double x = 3.3, y = 1.1;
printf (
"Ceil of %.1lf = %.1lf \nFloor of %.1lf = %.1lf\n" ,
x, ceil (x), x, floor (x));
printf ( "%.1lf raised to the power %.1lf = %.1lf\n" , x,
y, pow (x, y));
return 0;
}
|
Output
Ceil of 3.3 = 4.0
Floor of 3.3 = 3.0
3.3 raised to the power 1.1 = 3.7
Commonly used math.h header file functions
ceil(x)
|
Returns the largest integer smaller than or equal to x.
|
floor(x)
|
Returns the smallest integer larger than or equal to x.
|
fabs(x)
|
Returns the absolute value of x.
|
sqrt(x)
|
Returns the square root of x.
|
cbrt(x)
|
Returns the cube root of x.
|
pow(x , y)
|
Returns the value of x raised to the power y.
|
exp(x)
|
Returns the value of e(Euler’s Number) raised to the power x.
|
fmod(x , y)
|
Returns the remainder of x divided by y.
|
log(x)
|
Returns the natural logarithm of x.
|
log10(x)
|
Returns the common logarithm of x.
|
modf(x , *y)
|
Returns the fractional component of x and sets the y to the integral component of x.
|
cos(x)
|
Returns the cosine of radian angle x.
|
sin(x)
|
Returns the sine of radian angle x.
|
tan(x)
|
Returns the tangent of radian angle x.
|
acos(x)
|
Returns the arc cosine of x in radians.
|
asin(x)
|
Returns the arc sine of x in radians.
|
atan(x)
|
Returns the arc tangent of x in radians.
|
cosh(x)
|
Returns the hyperbolic cosine of x.
|
sinh(x)
|
Returns the hyperbolic sine of x.
|
math.h Library Macros
They are the macros defined inside <math.h> header file using #define preprocessor. They can be either constant that is replaced by their value when used or either function-like macros.
Example:
C
#include <math.h>
#include <stdio.h>
int main()
{
printf ( "Value Returned by HUGE_VAL = %.1lf\n" , HUGE_VAL);
printf ( "Value returned by isgreater = %d" , isgreater(12.0, 13.0));
return 0;
}
|
Output
Value Returned by HUGE_VAL = inf
Value returned by isgreater = 0
EXAMPLE 2 :
The math.h library in C provides a set of functions for performing mathematical operations. Here are some examples of functions from the math.h library, along with code samples:
1 .double cos(double x): This function returns the cosine of x, where x is an angle in radians.
C
#include <math.h>
#include <stdio.h>
int main( void )
{
double x = 1.5;
double y = 2.0;
double z;
z = sin (x);
printf ( "sin(%.2lf) = %.2lf\n" , x, z);
z = cos (x);
printf ( "cos(%.2lf) = %.2lf\n" , x, z);
z = tan (x);
printf ( "tan(%.2lf) = %lf\n" , x, z);
z = asin (x);
printf ( "asin(%.2lf) = %lf\n" , x, z);
z = acos (x);
printf ( "acos(%.2lf) = %lf\n" , x, z);
z = atan (x);
printf ( "atan(%.2lf) = %lf\n" , x, z);
z = exp (y);
printf ( "exp(%.2lf) = %lf\n" , y, z);
z = log (y);
printf ( "log(%.2lf) = %lf\n" , y, z);
z = sqrt (y);
printf ( "sqrt(%.2lf) = %lf\n" , y, z);
return 0;
}
|
Output:
sin(1.50) = 0.997495
cos(1.50) = 0.070737
tan(1.50) = 14.101420
asin(1.50) = nan
acos(1.50) = nan
atan(1.50) = 0.982794
exp(2.00) = 7.389056
log(2.00) = 0.693147
sqrt(2.00) = 1.414214
Note: inf is used to represent positive infinity (very large number that cannot be represented using double or float.
Here is a list of some commonly used math.h library macros:
HUGE_VAL
|
Represent positive infinity i.e. values too big to represent using a float, or double.
|
isgreater(x, y)
|
Checks if x > y and return true/false accordingly.
|
isless(x, y)
|
Checks if x < y and return true/false accordingly.
|
Note: isgreater(x , y) may look like a function but they are defined as macros in the math.h header file and are replaced by their value in preprocessing.
Share your thoughts in the comments
Please Login to comment...