C Math Library (math.h
)
The <math.h>
library in C offers a comprehensive set of functions for performing mathematical operations. From basic arithmetic to complex trigonometric and logarithmic calculations, this library is essential for developers looking to implement advanced math functionalities in their programs.
C Math Library (math.h
)
The <math.h>
library provides a wide range of functions for performing mathematical operations on numbers. These functions can handle everything from basic arithmetic to advanced mathematical operations such as trigonometric and logarithmic calculations.
Common C Math Functions
Below is a table of common functions in the math.h
library, along with their descriptions:
Function | Description |
---|---|
acos(x) |
Returns the arccosine of x, in radians. |
acosh(x) |
Returns the hyperbolic arccosine of x. |
asin(x) |
Returns the arcsine of x, in radians. |
asinh(x) |
Returns the hyperbolic arcsine of x. |
atan(x) |
Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians. |
atan2(y, x) |
Returns the angle from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). |
atanh(x) |
Returns the hyperbolic arctangent of x. |
cbrt(x) |
Returns the cube root of x. |
ceil(x) |
Returns the value of x rounded up to its nearest integer. |
cos(x) |
Returns the cosine of x (in radians). |
exp(x) |
Returns the value of ex . |
fabs(x) |
Returns the absolute value of x. |
floor(x) |
Returns the value of x rounded down to its nearest integer. |
log(x) |
Returns the natural logarithm (base e ) of x. |
pow(x, y) |
Returns the value of xy . |
sin(x) |
Returns the sine of x (in radians). |
sqrt(x) |
Returns the square root of x. |
tan(x) |
Returns the tangent of x (in radians). |
round(x) |
Returns x rounded to the nearest integer. |
Example: Basic Math Operations Using math.h
Here is a simple example that demonstrates the use of some common math functions in C:
Example: Using C Math Functions
#include <stdio.h>
#include <math.h>
int main() {
double x = 9.0;
double result;
// Calculate square root
result = sqrt(x);
printf("Square root of %.1f is %.2f\n", x, result);
// Calculate power
result = pow(x, 3);
printf("%.1f raised to the power of 3 is %.2f\n", x, result);
// Calculate sine
result = sin(x);
printf("Sine of %.1f is %.2f\n", x, result);
return 0;
}
Output:
Output
Square root of 9.0 is 3.00
9.0 raised to the power of 3 is 729.00
Sine of 9.0 is 0.41
This program calculates the square root, the power, and the sine of the number 9.0, using functions from the math.h
library.