C Standard Library -
Introduction
math.h The header file defines various mathematical functions and a macro. All available features in this library take a double type argument and all return a double type result.
is a header file in the C Standard Library that contains a large number of functions and macros for mathematical operations. These functions and macros provide basic arithmetic operations, trigonometric functions, exponential functions, logarithmic functions, power functions, rounding functions, and so on.
Library macros
in In math.h, some macros are used to represent the error status of mathematical functions:
| macro | Description |
|---|---|
HUGE_VAL |
The value returned when a function's result overflows (positive infinity). This macro represents a very large double-precision floating-point number, typically used as the return value of certain mathematical functions when the result exceeds the representable range. When a function's result is too large to be represented by a normal floating-point number (i.e., overflow occurs), errno is set to ERANGE (range error), and HUGE_VAL or its negative value (for negative infinity) is returned. |
HUGE_VALF |
The value returned when a function's result overflows (positive infinity, float type) |
HUGE_VALL |
The value returned when a function's result overflows (positive infinity, long double) |
INFINITY |
Positive infinity |
NAN |
Not-a-number value (Not-A-Number) |
FP_INFINITE |
Represents infinity |
FP_NAN |
Represents a not-a-number value |
FP_NORMAL |
Represents a normal floating-point number |
FP_SUBNORMAL |
Represents a subnormal number |
FP_ZERO |
Represents zero |
Library functions
The functions defined in the header file math.h are listed below:
| No. | Function & Description |
|---|---|
| 1 | double acos(double x) |
| Returns the arc cosine of x, expressed in radians. | |
| 2 | double asin(double x) |
| Returns the arc sine of x, expressed in radians. | |
| 3 | double atan(double x) |
| Returns the arc tangent of x, expressed in radians. | |
| 4 | double atan2(double y, double x) |
| Returns the arc tangent of y/x, expressed in radians. The signs of the values of y and x determine the correct quadrant. | |
| 5 | double cos(double x) |
| Returns the cosine of the angle x in radians. | |
| 6 | double cosh(double x) |
| Returns the hyperbolic cosine of x. | |
| 7 | double sin(double x) |
| Returns the sine of the angle x in radians. | |
| 8 | double sinh(double x) |
| Returns the hyperbolic sine of x. | |
| 9 | double tanh(double x) |
| Returns the hyperbolic tangent of x. | |
| 10 | double exp(double x) |
| Returns the value of e raised to the power of x. | |
| 11 | double frexp(double x, int *exponent) |
| Breaks the floating-point number x into a mantissa and an exponent. The return value is the mantissa, and the exponent is stored in exponent. The resulting value is x = mantissa * 2 ^ exponent. | |
| 12 | double ldexp(double x, int exponent) |
| Returns x multiplied by 2 raised to the power of exponent. | |
| 13 | double log(double x) |
| Returns the natural logarithm of x (logarithm to the base e). | |
| 14 | double log10(double x) |
| Returns the common logarithm of x (logarithm to the base 10). | |
| 15 | double modf(double x, double *integer) |
| The return value is the fractional part (the part after the decimal point), and integer is set to the integral part. | |
| 16 | double pow(double x, double y) |
| Returns x raised to the power of y. | |
| 17 | double sqrt(double x) |
| Returns the square root of x. | |
| 18 | double ceil(double x) |
| Returns the smallest integer value greater than or equal to x. | |
| 19 | double fabs(double x) |
| Returns the absolute value of x. | |
| 20 | double floor(double x) |
| Returns the largest integer value less than or equal to x. | |
| 21 | double fmod(double x, double y) |
| Returns the remainder of x divided by y. |
Common mathematical constants
The following are Some commonly used mathematical constants defined in math.h:
| Constant | Value | Description |
|---|---|---|
M_PI |
3.14159265358979323846 | Pi (π) |
M_E |
2.71828182845904523536 | The base of the natural logarithm, e |
M_LOG2E |
1.44269504088896340736 | log2(e) |
M_LOG10E |
0.43429448190325182765 | log10(e) |
M_LN2 |
0.69314718055994530942 | ln(2) |
M_LN10 |
2.30258509299404568402 | ln(10) |
M_PI_2 |
1.57079632679489661923 | π/2 |
M_PI_4 |
0.78539816339744830962 | π/4 |
M_1_PI |
0.31830988618379067154 | 1/π |
M_2_PI |
0.63661977236758134308 | 2/π |
M_2_SQRTPI |
1.12837916709551257390 | 2/√π |
M_SQRT2 |
1.41421356237309504880 | √2 |
M_SQRT1_2 |
0.70710678118654752440 | 1/√2 |
The following is how to use Example code for the functions and constants in math.h:
Example
#include
#include
int
main
(
)
{
double
x
=
2.0
;
double
y
=
3.0
;
// Basic operations
printf
(
"sqrt(%.1f) = %.2f\n"
,
x
,
sqrt
(
x
)
)
;
printf
(
"pow(%.1f, %.1f) = %.2f\n"
,
x
,
y
,
pow
(
x
,
y
)
)
;
printf
(
"exp(%.1f) = %.2f\n"
,
x
,
exp
(
x
)
)
;
printf
(
"log(%.1f) = %.2f\n"
,
x
,
log
(
x
)
)
;
printf
(
"log10(%.1f) = %.2f\n"
,
x
,
log10
(
x
)
)
;
// Trigonometric functions
double
angle
=
M_PI
/
4
;
// 45 degrees
printf
(
"sin(%.2f) = %.2f\n"
,
angle
,
sin
(
angle
)
)
;
printf
(
"cos(%.2f) = %.2f\n"
,
angle
,
cos
(
angle
)
)
;
printf
(
"tan(%.2f) = %.2f\n"
,
angle
,
tan
(
angle
)
)
;
// Rounding functions
double
z
=
5.8
;
printf
(
"ceil(%.1f) = %.1f\n"
,
z
,
ceil
(
z
)
)
;
printf
(
"floor(%.1f) = %.1f\n"
,
z
,
floor
(
z
)
)
;
printf
(
"fabs(%.1f) = %.1f\n"
,
z
,
fabs
(
z
)
)
;
return
0
;
}
Running the above program will produce output similar to the following:
sqrt(2.0) = 1.41
pow(2.0, 3.0) = 8.00
exp(2.0) = 7.39
log(2.0) = 0.69
log10(2.0) = 0.30
sin(0.79) = 0.71
cos(0.79) = 0.71
tan(0.79) = 1.00
ceil(5.8) = 6.0
floor(5.8) = 5.0
fabs(5.8) = 5.8
Provides a series of mathematical functions and constants that help programmers perform various mathematical computations in C. By using these functions and constants, complex mathematical operations can be easily implemented, thereby improving the efficiency and readability of programs.