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:

MacroDescription
HUGE_VALThe 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_VALFThe value returned when the function result overflows (positive infinity, floating-point type)
HUGE_VALLThe value returned when the function result overflows (positive infinity, long double precision)
INFINITYPositive infinity
NANA non-number value (Not-A-Number)
FP_INFINITERepresents infinity
FP_NANRepresents a non-numeric value
FP_NORMALRepresents a normal floating-point number
FP_SUBNORMALRepresents a subnormal number
FP_ZERORepresents zero

Library functions

The following lists the functions defined in the header file math.h:

Serial NumberFunction & Description
1double acos(double x)
Returns the arc cosine of x in radians.
2double asin(double x)
Returns the arc sine of x in radians.
3double atan(double x)
Returns the arc tangent of x in radians.
4double 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.
5double cos(double x)
Returns the cosine of an angle x in radians.
6double cosh(double x)
Returns the hyperbolic cosine of x.
7double sin(double x)
Returns the sine of an angle x in radians.
8double sinh(double x)
Returns the hyperbolic sine of x.
9double tanh(double x)
Returns the hyperbolic tangent of x.
10double exp(double x)
Returns e raised to the power of x.
11double 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.
12double ldexp(double x, int exponent)
Returns x multiplied by 2 raised to the power of exponent.
13double log(double x)
Returns the natural logarithm of x (logarithm to base e).
14double log10(double x)
Returns the common logarithm of x (logarithm to base 10).
15double 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.
16double pow(double x, double y)
Returns x raised to the power of y.
17double sqrt(double x)
Returns the square root of x.
18double ceil(double x)
Returns the smallest integer value greater than or equal to x.
19double fabs(double x)
Returns the absolute value of x.
20double floor(double x)
Returns the largest integer value less than or equal to x.
21double 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:

ConstantsValueDescription
M_PI3.14159265358979323846Pi π
M_E2.71828182845904523536The base of natural logarithms, e
M_LOG2E1.44269504088896340736log2(e)
M_LOG10E0.43429448190325182765log10(e)
M_LN20.69314718055994530942ln(2)
M_LN102.30258509299404568402ln(10)
M_PI_21.57079632679489661923π/2
M_PI_40.78539816339744830962π/4
M_1_PI0.318309886183790671541/π
M_2_PI0.636619772367581343082/π
M_2_SQRTPI1.128379167095512573902/√π
M_SQRT21.41421356237309504880√2
M_SQRT1_20.707106781186547524401/√2

The following is using<math.h>Example code for the functions and constants in:

Example

#include <stdio.h>
#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.

other extensions