C++ <cmath> Math Functions

Beginner
8 min read
Updated: Nov 2026
3 examples
<cmath>

Definition and Usage

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.

This page teaches what <cmath> is. The Math Functions index lists every function guide in this series, for example sqrt(), pow(), and floor().

Do I need -lm?

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.

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

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:

DegreesRadiansstd::sin()std::cos()
000.00001.0000
300.5235990.50000.8660
450.7853980.70710.7071
601.04720.86600.5000
901.57081.00000.0000
1803.141590.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.

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:

xceil(x)floor(x)trunc(x)round(x)
-2.5-2-3-2-3
-2.3-2-3-2-2
2.53223
2.73223

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>.

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::.

C++
#include <cmath>
#include <iostream>

int main() {
    double side = 12.0;

    std::cout << "sqrt(144)    = " << std::sqrt(144.0) << '\n';
    std::cout << "pow(2, 8)    = " << std::pow(2.0, 8.0) << '\n';
    std::cout << "abs(-7.5)    = " << std::abs(-7.5) << '\n';
    std::cout << "ceil(4.2)    = " << std::ceil(4.2) << '\n';
    std::cout << "floor(4.8)   = " << std::floor(4.8) << '\n';
    std::cout << "round(side / 5) = " << std::round(side / 5) << '\n';
    return 0;
}

How It Works

  1. std::sqrt(144.0) finds the number that times itself gives 144, so the answer is 12.
  2. std::pow(2.0, 8.0) multiplies two eight times, giving 256. std::abs(-7.5) drops the minus sign.
  3. ceil(4.2) goes up to 5 and floor(4.8) goes down to 4. round(12 / 5) turns 2.4 into 2. Whole values print without a decimal point.

Example 2 — Sine and Cosine with Radians

Trigonometric functions need radians. This program converts degrees first, then prints the sine and cosine with four decimals.

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    const double toRadians = std::numbers::pi / 180.0;

    std::cout << std::fixed << std::setprecision(4);
    for (int deg : {0, 30, 60, 90}) {
        double rad = deg * toRadians;
        std::cout << deg << " deg: sin = " << std::sin(rad)
                  << ", cos = " << std::cos(rad) << '\n';
    }
    return 0;
}

How It Works

  1. 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.
  2. The loop multiplies each angle in degrees by that factor, then passes the radians to std::sin() and std::cos().
  3. 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;
}

How It Works

  1. A negative number has no real square root, so std::sqrt(-4.0) returns NaN. std::isnan() prints 1 for true.
  2. 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.
  3. 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.

Next: C++ Math Functions Index

After you know what <cmath> is, open the function index for a full tutorial on each function.

Start withWhy
sqrt()Find a square root, and learn what NaN means
pow()Raise a number to a power
floor()Round down to a whole number
isnan()Check a result for NaN before you use it

Full list: C++ Math Functions index (/cpp/math/function).

Frequently Asked Questions

<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.

Browse every math function

Open the functions index for categorized guides with examples and FAQs.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful