C Standard Library -<math.h>
Introduction
math.hThe header file defines various mathematical functions and a macro. All available functionality in this library is accompanied by adoubleparameters of the type, and all returndoubleResult of type.
<math.h>It is a header file in the C standard library, containing a large number of functions and macros used for mathematical operations. These functions and macros provide basic arithmetic operations, trigonometric functions, exponential functions, logarithmic functions, power functions, rounding functions, etc.
Library macros
In<math.h>Among them, some macros are used to represent the error status of mathematical functions:
| Macro | Description |
|---|---|
HUGE_VAL | The value returned when the function 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 as 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 the function result overflows (positive infinity, floating-point type) |
HUGE_VALL | The value returned when the function result overflows (positive infinity, long double precision) |
INFINITY | Positive infinity |
NAN | A non-number value (Not-A-Number) |
FP_INFINITE | Represents infinity |
FP_NAN | Represents a non-numeric value |
FP_NORMAL | Represents a normal floating-point number |
FP_SUBNORMAL | Represents a subnormal number |
FP_ZERO | Represents zero |
Library functions
The following lists the functions defined in the header file math.h:
| Serial Number | Function & Description |
|---|---|
| 1 | double acos(double x) Returns the arc cosine of x in radians. |
| 2 | double asin(double x) Returns the arc sine of x in radians. |
| 3 | double atan(double x) Returns the arc tangent of x in radians. |
| 4 | double atan2(double y, double x) Returns the arc tangent of y/x in radians. The signs of the values of y and x determine the correct quadrant. |
| 5 | double cos(double x) Returns the cosine of an angle x in radians. |
| 6 | double cosh(double x) Returns the hyperbolic cosine of x. |
| 7 | double sin(double x) Returns the sine of an 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 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 base e). |
| 14 | double log10(double x) Returns the common logarithm of x (logarithm to base 10). |
| 15 | double modf(double x, double *integer) The return value is the fractional part (the part after the decimal point), and sets integer as the integer 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<math.h>Some commonly used mathematical constants defined in:
| Constants | Value | Description |
|---|---|---|
M_PI | 3.14159265358979323846 | Pi π |
M_E | 2.71828182845904523536 | The base of natural logarithms, 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 using<math.h>Example code for the functions and constants in:
Example
#include <math.h>
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
<math.h>It provides a series of mathematical functions and constants to help programmers perform various mathematical calculations in C. By using these functions and constants, complex mathematical operations can be easily implemented, thereby improving the efficiency and readability of the program.