C++ Math Functions

About This Index
This page is the C++ Math Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what <cmath> is on /cpp/math. Use this page when you need a specific function such as sqrt(), pow(), or floor().
All of these functions live in <cmath> and in the std namespace, so you write std::sqrt(x). Most take a double, and angles are in radians. A bad input gives NaN or infinity instead of a crash. The 70 functions are listed below.
How to Call a Math Function
Include <cmath>, then call the function with std:: in front:
#include <cmath>
#include <iostream>
double root = std::sqrt(16.0); // 4
double power = std::pow(2.0, 10.0); // 1024
double dist = std::abs(-7.5); // 7.5
double down = std::floor(3.9); // 3
double up = std::ceil(3.1); // 4
double sine = std::sin(0.0); // 0 (radians)
bool bad = std::isnan(std::sqrt(-1.0)); // true C++ Math Functions Table
All function guides in this C++ tutorial series (70 tutorials), grouped by purpose.
Absolute value and sign
4 functionsRemove a minus sign, copy a sign, or ask whether a number is negative.
Roots and powers
4 functionsSquare roots, cube roots, powers, and the length of a right triangle side.
Exponential and logarithm
9 functionsPowers of e and of 2, and logarithms in base e, 10, and 2.
| Function | Description | Tutorial |
|---|---|---|
exp() | The number e raised to a power. | Try it |
exp2() | Two raised to a power. | Try it |
expm1() | e to the power x, minus 1, with high accuracy. | Try it |
log() | Natural logarithm (base e) of a number. | Try it |
log10() | Base-10 logarithm of a number. | Try it |
log2() | Base-2 logarithm of a number. | Try it |
log1p() | Natural logarithm of 1 plus x, with high accuracy. | Try it |
logb() | The binary exponent of a number, as a double. | Try it |
ilogb() | The binary exponent of a number, as an int. | Try it |
Trigonometry
7 functionsSine, cosine, tangent, and their inverses. Angles are in radians.
| Function | Description | Tutorial |
|---|---|---|
sin() | Sine of an angle in radians. | Try it |
cos() | Cosine of an angle in radians. | Try it |
tan() | Tangent of an angle in radians. | Try it |
asin() | Inverse sine, as an angle in radians. | Try it |
acos() | Inverse cosine, as an angle in radians. | Try it |
atan() | Inverse tangent, as an angle in radians. | Try it |
atan2() | Angle of a point (x, y), in the correct quadrant. | Try it |
Hyperbolic functions
6 functionsHyperbolic sine, cosine, tangent, and their inverses.
| Function | Description | Tutorial |
|---|---|---|
sinh() | Hyperbolic sine of a number. | Try it |
cosh() | Hyperbolic cosine of a number. | Try it |
tanh() | Hyperbolic tangent of a number. | Try it |
asinh() | Inverse hyperbolic sine of a number. | Try it |
acosh() | Inverse hyperbolic cosine of a number. | Try it |
atanh() | Inverse hyperbolic tangent of a number. | Try it |
Rounding
11 functionsTurn a double into a whole number: ceil, floor, trunc, round, and the rint family.
| Function | Description | Tutorial |
|---|---|---|
ceil() | Round a number up to the next whole number. | Try it |
floor() | Round a number down to the next whole number. | Try it |
trunc() | Cut off the decimal part of a number. | Try it |
round() | Round a number to the nearest whole number. | Try it |
nearbyint() | Round using the current rounding mode. | Try it |
rint() | Round using the current rounding mode. | Try it |
lrint() | Round to the nearest long integer. | Try it |
llrint() | Round to the nearest long long integer. | Try it |
lround() | Round to the nearest long, halves away from zero. | Try it |
llround() | Round to the nearest long long, halves away from zero. | Try it |
modf() | Split a number into whole and fractional parts. | Try it |
Remainder, scale, and compare
13 functionsRemainders, powers of two, neighbours, fused multiply-add, and larger or smaller of two numbers.
| Function | Description | Tutorial |
|---|---|---|
fmod() | Remainder of a division of two decimal numbers. | Try it |
remainder() | IEEE remainder: x minus the nearest multiple of y. | Try it |
remquo() | IEEE remainder plus the low bits of the quotient. | Try it |
frexp() | Split a number into a mantissa and a power of two. | Try it |
ldexp() | Multiply a number by a power of two. | Try it |
scalbn() | Multiply a number by the radix raised to an int power. | Try it |
scalbln() | Like scalbn(), but the power is a long. | Try it |
nextafter() | The next representable number after x, toward y. | Try it |
nexttoward() | Like nextafter(), but the direction is a long double. | Try it |
fma() | Multiply and add in one step: x * y + z. | Try it |
fdim() | Positive difference of two numbers (never below 0). | Try it |
fmax() | The larger of two numbers, ignoring NaN. | Try it |
fmin() | The smaller of two numbers, ignoring NaN. | Try it |
Classification and comparison
12 functionsCheck for NaN, infinity, and normal numbers, and compare safely when NaN is possible.
| Function | Description | Tutorial |
|---|---|---|
fpclassify() | Tells whether a number is normal, zero, subnormal, infinite, or NaN. | Try it |
isfinite() | Checks that a number is not infinity and not NaN. | Try it |
isinf() | Checks if a number is positive or negative infinity. | Try it |
isnan() | Checks if a number is NaN (not a number). | Try it |
isnormal() | Checks that a number is normal: not zero, subnormal, inf, or NaN. | Try it |
isgreater() | Checks x > y safely, false if either is NaN. | Try it |
isgreaterequal() | Checks x >= y safely, false if either is NaN. | Try it |
isless() | Checks x < y safely, false if either is NaN. | Try it |
islessequal() | Checks x <= y safely, false if either is NaN. | Try it |
islessgreater() | Checks x < y or x > y, false if either is NaN. | Try it |
isunordered() | Checks if x and y cannot be compared because one is NaN. | Try it |
nan() | Creates a quiet NaN (not a number) value. | Try it |
Special functions
4 functionsThe error function and the gamma function.
| Function | Description | Tutorial |
|---|---|---|
erf() | The error function, used in statistics and probability. | Try it |
erfc() | The complementary error function, 1 - erf(x), kept accurate. | Try it |
tgamma() | The gamma function: a smooth factorial for decimal numbers. | Try it |
lgamma() | The natural logarithm of the gamma function, for big factorials. | Try it |
Frequently Asked Questions
Start with sqrt()
Take the square root of a number, the most common cmath function.
12 people found this page helpful
