The C++ standard library
The C++ standard library provides rich functionality, among which is a header file containing math functions; it provides many basic mathematical operations and constants.
is a header file in the C++ standard library that defines a set of math functions capable of performing basic mathematical operations such as exponentiation, trigonometric functions, logarithms, absolute values, etc.
To use 's functions, you need to include this header in your C++ program:
#include
Common functions
provides many math functions; here are some commonly used ones.
1. Basic math functions
| function | Function | Example |
|---|---|---|
abs(x) |
Compute integer x 's absolute value |
abs(-5) // 5 |
fabs(x) |
Compute floating-point x 's absolute value |
fabs(-5.5) // 5.5 |
fmod(x, y) |
Compute x divided by y 's remainder |
fmod(5.3, 2) // 1.3 |
remainder(x, y) |
Compute x divided by y 's remainder |
remainder(5.5, 2) // 1.5 |
fmax(x, y) |
Return x and y the larger of |
fmax(3.5, 4.2) // 4.2 |
fmin(x, y) |
Return x and y the smaller of |
fmin(3.5, 4.2) // 3.5 |
hypot(x, y) |
Compute sqrt(x*x + y*y) |
hypot(3, 4) // 5 |
2. Exponential and logarithmic functions
| function | Function | Example |
|---|---|---|
exp(x) |
Compute e^x, e is the base of the natural logarithm |
exp(1) // 2.71828... |
log(x) |
Compute x 's natural logarithm |
log(2.71828) // 1 |
log10(x) |
Compute x 's base-10 logarithm |
log10(100) // 2 |
pow(x, y) |
Compute x 's y to the power of |
pow(2, 3) // 8 |
sqrt(x) |
Compute x 's square root |
sqrt(16) // 4 |
cbrt(x) |
Compute x 's cube root |
cbrt(27) // 3 |
expm1(x) |
Compute e^x - 1 |
expm1(1) // 1.71828... |
log1p(x) |
Compute log(1 + x), suitable when x is close to 0 |
log1p(0.00001) // 0.00001 |
3. Trigonometric functions
| function | Function | Example |
|---|---|---|
sin(x) |
Compute x 's sine value,x in radians |
sin(3.14159 / 2) // 1 |
cos(x) |
Compute x 's cosine value,x in radians |
cos(3.14159) // -1 |
tan(x) |
Compute x 's tangent value,x in radians |
tan(0) // 0 |
asin(x) |
Compute x 's arcsine value, returns radians |
asin(1) // 3.14159/2 |
acos(x) |
Compute x 's arccosine value, returns radians |
acos(-1) // 3.14159 |
atan(x) |
Compute x 's arctangent value, returns radians |
atan(1) // 3.14159/4 |
atan2(y, x) |
Compute y/x 's arctangent value, returns radians |
atan2(1, 1) // 3.14159/4 |
4. Hyperbolic functions
| function | Function | Example |
|---|---|---|
sinh(x) |
Compute x 's hyperbolic sine |
sinh(0) // 0 |
cosh(x) |
Compute x 's hyperbolic cosine |
cosh(0) // 1 |
tanh(x) |
Compute x 's hyperbolic tangent |
tanh(1) // 0.7616 |
asinh(x) |
Compute x 's inverse hyperbolic sine |
asinh(1) // 0.8814 |
acosh(x) |
Compute x 's inverse hyperbolic cosine,x ≥ 1 |
acosh(1) // 0 |
atanh(x) |
Compute x 's inverse hyperbolic tangent,x in (-1, 1) |
atanh(0.5) // 0.5493 |
5. Rounding and floating-point operations
| function | Function | Example |
|---|---|---|
ceil(x) |
Returns the smallest integer not less than x the minimum integer |
ceil(2.3) // 3 |
floor(x) |
Returns the largest integer not greater than x the maximum integer |
floor(2.3) // 2 |
trunc(x) |
Returns the integer value with the fractional part removed | trunc(2.8) // 2 |
round(x) |
Returns the integer rounded to the nearest | round(2.5) // 3 |
lround(x) |
Returns rounded to long Type |
lround(2.5) // 3 |
llround(x) |
Returns rounded to long long Type |
llround(2.5) // 3 |
nearbyint(x) |
Returns rounded to the nearest integer (without raising floating-point exceptions) | nearbyint(2.5) // 2 |
rint(x) |
Returns rounded to an integer according to the current rounding mode | rint(2.5) // 3 |
modf(x, &intpart) |
data to the child component via x 's integer and fractional parts separately |
modf(2.3, &intpart) |
6. Floating-point checks
| function | Function | Example |
|---|---|---|
isfinite(x) |
Checks x whether is a finite value (not infinity or NaN) |
isfinite(3.0) // true |
isinf(x) |
Checks x whether is infinity |
isinf(1.0 / 0.0) // true |
isnan(x) |
Checks x whether is NaN |
isnan(0.0 / 0.0) // true |
isnormal(x) |
Checks x whether is a normal non-zero floating-point number |
isnormal(1.0) // true |
signbit(x) |
Checks x 's sign is negative |
signbit(-5.3) // true |
Example
Below is an example using is a simple example demonstrating how to compute the square root, sine, and absolute value of a number.
Example 1
#include
#include
int
main
(
)
{
double
num
=
9.0
;
double
root
=
sqrt
(
num
)
;
// Compute square root
double
sinValue
=
sin
(
M_PI
/
2
)
;
// Compute sine; M_PI is an approximation of π
double
absValue
=
abs
(
5.0
)
;
// Compute 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 result:
The square root of 9 is 3
The sine of 1.570796 is 1
The absolute value of -5 is 5
Example 2
#include
#include
int
main
(
)
{
// Basic math 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
's functions usually acceptfloatordoubletype arguments and return results of the corresponding type. Forlong doubletype, you can use's functions, but you need to appendlsuffix, for examplesqrtl.- Certain functions, such as
powandlog, can accept integers as arguments, but the result is still floating-point. 's functions may throw exceptions, for example when thesqrtfunction's argument is negative, it throwsstd::domain_errorexception.