C++ Standard Library<cmath>

The C++ standard library provides a wealth of functionality, among which<cmath>is a header file containing mathematical functions, providing many basic mathematical operations and constants.

<cmath>is a header file in the C++ standard library that defines a set of mathematical functions capable of performing basic mathematical operations such as exponentiation, trigonometric functions, logarithms, absolute values, etc.

To use<cmath>For the functions in it, you need to include this header file in your C++ program:

#include <cmath>

Common Functions

<cmath>provides many mathematical functions; here are some commonly used ones.

1. Basic Mathematical Functions

FunctionFeaturesExample
abs(x)Compute an integerx's absolute valueabs(-5) // 5
fabs(x)Compute a floating-point numberx's absolute valuefabs(-5.5) // 5.5
fmod(x, y)Computexdivided byythe remainderfmod(5.3, 2) // 1.3
remainder(x, y)Computexdivided byythe remainderremainder(5.5, 2) // 1.5
fmax(x, y)returnxandythe larger valuefmax(3.5, 4.2) // 4.2
fmin(x, y)returnxandythe smaller valuefmin(3.5, 4.2) // 3.5
hypot(x, y)Computesqrt(x*x + y*y)hypot(3, 4) // 5

2. Exponential and Logarithmic Functions

FunctionFeaturesExample
exp(x)Computee^x,ebase of the natural logarithmexp(1) // 2.71828...
log(x)Computex's natural logarithmlog(2.71828) // 1
log10(x)Computexbase-10 logarithm oflog10(100) // 2
pow(x, y)Computexofypowerpow(2, 3) // 8
sqrt(x)Computex's square rootsqrt(16) // 4
cbrt(x)Computex's cube rootcbrt(27) // 3
expm1(x)Computee^x - 1expm1(1) // 1.71828...
log1p(x)Computelog(1 + x), suitable for cases where x is close to 0log1p(0.00001) // 0.00001

3. Trigonometric functions

Function Features Example
sin(x) Computexsine of,xIn radians sin(3.14159 / 2) // 1
cos(x) Computexcosine of,xIn radians cos(3.14159) // -1
tan(x) Computextangent of,xIn radians tan(0) // 0
asin(x) Computexarcsine of, returns radians asin(1) // 3.14159/2
acos(x) Computexarccosine of, returns radians acos(-1) // 3.14159
atan(x) Computexarctangent of, returns radians atan(1) // 3.14159/4
atan2(y, x) Computey/xarctangent of, returns radians atan2(1, 1) // 3.14159/4

4. Hyperbolic functions

Function Features Example
sinh(x) Computexhyperbolic sine of sinh(0) // 0
cosh(x) Computexhyperbolic cosine of cosh(0) // 1
tanh(x) Computexhyperbolic tangent of tanh(1) // 0.7616
asinh(x) Computexinverse hyperbolic sine of asinh(1) // 0.8814
acosh(x) Computexinverse hyperbolic cosine of,x ≥ 1 acosh(1) // 0
atanh(x) Computexinverse hyperbolic tangent of,xon (-1, 1) atanh(0.5) // 0.5493

5. Rounding and Floating-Point Operations

Function Features Example
ceil(x) Returns not less thanxthe smallest integer ceil(2.3) // 3
floor(x) Returns not greater thanxthe largest integer floor(2.3) // 2
trunc(x) Returns the integer value with the fractional part removed trunc(2.8) // 2
round(x) Returns the nearest integer after rounding round(2.5) // 3
lround(x) Returns rounded tolongType lround(2.5) // 3
llround(x) Returns rounded tolong longType llround(2.5) // 3
nearbyint(x) returns rounding to the nearest integer (but does not raise floating-point exceptions) nearbyint(2.5) // 2
rint(x) returns rounding to an integer according to the current rounding mode rint(2.5) // 3
modf(x, &intpart) willxSeparate the integer and fractional parts of modf(2.3, &intpart)

6. Floating-Point Checks

Function Features Example
isfinite(x) Checksxwhether it is a finite value (not infinity or NaN) isfinite(3.0) // true
isinf(x) Checksxwhether it is infinity isinf(1.0 / 0.0) // true
isnan(x) Checksxwhether it is NaN isnan(0.0 / 0.0) // true
isnormal(x) ChecksxWhether it is a normal non-zero floating-point number isnormal(1.0) // true
signbit(x) Checksxwhether the sign is negative signbit(-5.3) // true

Example

Below is an example using<cmath>A simple example of , demonstrating how to compute the square root, sine, and absolute value of a number.

Example 1

#include <iostream>
#include <cmath> // include the <cmath> header file

int main() {
    double num = 9.0;
    double root = sqrt(num); // calculate square root
    double sinValue = sin(M_PI / 2); // Calculate the sine value, M_PI is an approximation of π
    double absValue = abs(-5.0); // Calculate the absolute value

    std::cout << "The square root of " << num << " is " << root << std::endl;
    std::cout << "The sine of " << M_PI / 2 << " is " << sinValue << std::endl;
    std::cout << "The absolute value of -5.0 is " << absValue << std::endl;

    return 0;
}

Output:

The square root of 9 is 3
The sine of 1.570796 is 1
The absolute value of -5 is 5

Example 2

#include <iostream>
#include <cmath>

int main() {
    // Basic mathematical operations
    std::cout << "abs(-5) = " << abs(-5) << std::endl;
    std::cout << "fmod(5.3, 2) = " << fmod(5.3, 2) << std::endl;

    // Exponential and logarithmic functions
    std::cout << "exp(1) = " << exp(1) << std::endl;
    std::cout << "log(2.71828) = " << log(2.71828) << std::endl;
    std::cout << "pow(2, 3) = " << pow(2, 3) << std::endl;

    // Trigonometric functions
    std::cout << "sin(3.14159 / 2) = " << sin(3.14159 / 2) << std::endl;
    std::cout << "cos(3.14159) = " << cos(3.14159) << std::endl;

    // Rounding functions
    std::cout << "ceil(2.3) = " << ceil(2.3) << std::endl;
    std::cout << "floor(2.3) = " << floor(2.3) << std::endl;

    // Floating-point checks
    double x = 1.0 / 0.0;
    if (isinf(x)) {
        std::cout << "x is infinite" << std::endl;
    }

    return 0;
}

Notes

  • <cmath>Functions in typically acceptfloatordoubletype arguments and return results of the corresponding type. Forlong doubletype, you can use<cmath>functions in , but you need to add after the function namelsuffix, for examplesqrtl。
  • Certain functions, such aspowandlog, you can pass integers as arguments, but the result is still a floating-point number.
  • <cmath>functions in may throw exceptions, for example whensqrtWhen the argument of the function is negative, it throwsstd::domain_erroran exception.
  • <cmath>is a very useful header file in the C++ standard library. It provides many basic mathematical functions that can help developers perform mathematical operations when writing programs.

    other extensions