C Library – math.h
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:
- Functions
- Macros
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
// C program to demonstrate the// use of math.h library // functions#include <math.h>#include <stdio.h> int main(){ // two variables for test double x = 3.3, y = 1.1; // ceil and floor demonstration printf( "Ceil of %.1lf = %.1lf \nFloor of %.1lf = %.1lf\n", x, ceil(x), x, floor(x)); // pow function demonstration printf("%.1lf raised to the power %.1lf = %.1lf\n", x, y, pow(x, y)); return 0;} |
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
Function Name | Function Description |
|---|---|
ceil(x) | Returns the largest integer smaller than or equal to x. |
floor(x) | Returns the smallest integer larger than or equal to 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
// C program to demonstrate // the use of math.h library// macros#include <math.h>#include <stdio.h> int main(){ // HUGE_VAL macro printf("Value Returned by HUGE_VAL = %.1lf\n", HUGE_VAL); // isgreater macro demonstration printf("Value returned by isgreater = %d", isgreater(12.0, 13.0)); return 0;} |
Value Returned by HUGE_VAL = inf Value returned by isgreater = 0
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:
Macro Name | Macro Description |
|---|---|
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.





Please Login to comment...