In C++, <cmath> is the standard header for math functions. It gives you square roots, powers, logarithms, sine and cosine, rounding, and more. You call each one as std::name(value).
Most functions have three versions, for float, double, and long double. C++ picks the one that matches your argument, so std::sqrt(2.0) returns a double and std::sqrt(2.0f) returns a float. If you pass an int, it is converted to double. Beginners usually work with double.
With g++ you normally do not. It links the math library by default. If you compile as C, or you see undefined reference to 'sin', add -lm at the end of the command, for example g++ main.cpp -o main -lm.
Syntax
C++ Syntax
Common ways to use <cmath>:
C++
#include <cmath>
#include <iostream>
#include <numbers> // C++20, for std::numbers::pi
double r = std::sqrt(25.0); // 5
double p = std::pow(3.0, 4.0); // 81
double s = std::sin(std::numbers::pi / 2); // 1 (radians)
double l = std::log(std::exp(1.0)); // 1
double d = std::fabs(-3.5); // 3.5
long n = std::lround(2.6); // 3, a whole number
float f = std::sqrt(2.0f); // float version
bool x = std::isnan(std::sqrt(-1.0)); // true
Reference
Radians and Degrees
People think in degrees, but std::sin(), std::cos(), and std::tan() expect radians. Convert with radians = degrees * std::numbers::pi / 180. Here are the common angles:
Degrees
Radians
std::sin()
std::cos()
0
0
0.0000
1.0000
30
0.523599
0.5000
0.8660
45
0.785398
0.7071
0.7071
60
1.0472
0.8660
0.5000
90
1.5708
1.0000
0.0000
180
3.14159
0.0000
-1.0000
If your compiler is older than C++20, write const double pi = 3.14159265358979323846; yourself. Some compilers also offer M_PI, but it is not part of the standard, so avoid it in portable code.
Guide
Rounding and abs: Which One to Pick
These four functions all turn a fraction into a whole number, but they give different answers for negative numbers and for .5:
abs() and fabs(): with <cmath>, std::abs() works for float, double, and long double. std::fabs() does the same for floating-point numbers. The integer versions of abs() for int and long are also in <cstdlib>.
Code
C++ Examples
Three short programs that show how <cmath> works.
Example 1 — Square Root, Power, and Rounding
A first program that uses six common functions from <cmath>. Each call starts with std::.
0 deg: sin = 0.0000, cos = 1.0000
30 deg: sin = 0.5000, cos = 0.8660
60 deg: sin = 0.8660, cos = 0.5000
90 deg: sin = 1.0000, cos = 0.0000
How It Works
std::numbers::pi / 180.0 is the number of radians in one degree. It needs <numbers> and C++20. On older compilers use 3.14159265358979 instead.
The loop multiplies each angle in degrees by that factor, then passes the radians to std::sin() and std::cos().
std::fixed and std::setprecision(4) show four decimals. Without them, cos(90) would print as 6.12323e-17 instead of 0.0000, because a double cannot hold pi exactly.
Example 3 — Bad Input: NaN and Infinity
Math functions do not crash on bad input. They return a special value. Check it with std::isnan() and std::isinf().
C++
#include <cmath>
#include <iostream>
int main() {
double a = std::sqrt(-4.0);
double b = std::log(0.0);
double c = std::pow(10.0, 400.0);
std::cout << "sqrt(-4) is NaN: " << std::isnan(a) << '\n';
std::cout << "log(0) is inf: " << std::isinf(b) << " value " << b << '\n';
std::cout << "pow(10, 400): " << c << '\n';
std::cout << "sqrt(4) is NaN: " << std::isnan(std::sqrt(4.0)) << '\n';
return 0;
}
Output
sqrt(-4) is NaN: 1
log(0) is inf: 1 value -inf
pow(10, 400): inf
sqrt(4) is NaN: 0
How It Works
A negative number has no real square root, so std::sqrt(-4.0) returns NaN. std::isnan() prints 1 for true.
std::log(0.0) is minus infinity, and std::pow(10.0, 400.0) is too large for a double, so it becomes inf. Neither stops the program.
A valid input such as std::sqrt(4.0) gives 2, so isnan() prints 0. Always check first when the input comes from a user.
Continue
Next: C++ Math Functions Index
After you know what <cmath> is, open the function index for a full tutorial on each function.
<cmath> is the standard header with math functions such as sqrt(), pow(), sin(), cos(), log(), floor(), and ceil(). In C++ these functions live in the std namespace, so you write std::sqrt(x).
With g++ you usually do not. It links the math library for you. If you compile with gcc or clang in C mode, or see "undefined reference to sin", add -lm at the end of the command.
Radians. std::sin(90.0) is not 1. Multiply degrees by std::numbers::pi / 180 first. std::numbers::pi needs C++20 and #include <numbers>.
In <cmath>, std::abs() has overloads for float, double, and long double, so it works like fabs(). std::fabs() is the older name. For int and long, abs() is also declared in <cstdlib>. Include <cmath> when you pass a floating-point value.
Open the C++ Math Functions index. It lists 70 functions, from abs() and sqrt() to tgamma(), each with its own tutorial.