TExcelTAD
Home › C++ › C++ Math

C++ Math

Beyond the basic arithmetic operators, C++ offers a large collection of mathematical functions in the <cmath> header, plus handy helpers such as std::max and std::min in <algorithm>.

Max and min

std::max(a, b) returns the larger of two values and std::min(a, b) the smaller. Both arguments must have the same type. With braces you can pass a whole list:

cpp · try it yourself
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    cout << max(8, 3) << "\n";
    cout << min(2.5, 1.5) << "\n";
    cout << max({4, 19, 7, 11}) << "\n";
}
8
1.5
19

Common <cmath> functions

Function Description Example Result
sqrt(x) Square root sqrt(81) 9
pow(x, y) x to the power y pow(2, 10) 1024
abs(x) Absolute value abs(-4.5) 4.5
round(x) Round to nearest round(2.5) 3
floor(x) Round down floor(2.9) 2
ceil(x) Round up ceil(2.1) 3
trunc(x) Drop the fraction trunc(-2.7) -2
hypot(x, y) √(x² + y²) hypot(3, 4) 5
log(x) / log10(x) Natural / base-10 logarithm log10(1000) 3
sin, cos, tan Trigonometry (radians) cos(0) 1
cpp · try it yourself
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    cout << sqrt(81) << "\n";
    cout << pow(2, 10) << "\n";
    cout << abs(-4.5) << "\n";
    cout << round(2.5) << " " << floor(2.9) << " " << ceil(2.1) << "\n";
    cout << trunc(-2.7) << " " << floor(-2.7) << "\n";
    cout << hypot(3, 4) << "\n";
    cout << log10(1000) << "\n";
}
9
1024
4.5
3 2 3
-2 -3
5
3

Notice that floor(-2.7) is -3 (it always goes down), while trunc(-2.7) is -2 (it goes toward zero).

A practical example: distance and area

Many real calculations combine several functions. Here we compute the distance between two points and the area of a circle. std::numbers::pi (C++20, header <numbers>) gives a precise value of π:

cpp · try it yourself
#include <iostream>
#include <cmath>
#include <numbers>
#include <iomanip>
using namespace std;

int main() {
    double x1 = 1, y1 = 2, x2 = 7, y2 = 10;
    double dist = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
    cout << "Distance: " << dist << "\n";

    double r = 2.5;
    double area = numbers::pi * r * r;
    cout << fixed << setprecision(3);
    cout << "Circle area: " << area << "\n";
}
Distance: 10
Circle area: 19.635

Integer tricks

For integers you usually do not need <cmath> at all:

  • n % 10 gives the last digit, n / 10 removes it.
  • (a + b - 1) / b divides and rounds up for positive integers (handy for "how many pages do I need?").
  • Avoid pow for integer powers in tight loops — it works with double. Multiply instead, e.g. x * x.
cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    int n = 4096;
    int digitSum = 0;
    while (n > 0) {
        digitSum += n % 10;
        n /= 10;
    }
    cout << "Digit sum: " << digitSum << "\n";

    int items = 23, perPage = 10;
    cout << "Pages: " << (items + perPage - 1) / perPage << "\n";
}
Digit sum: 19
Pages: 3

Tip: Trigonometric functions use radians, not degrees. Convert with degrees * numbers::pi / 180.

Test yourself

Which header provides sqrt and pow?

  1. <math>
  2. <cmath>
  3. <numeric>
  4. <algorithm>

What is floor(-1.5)?

  1. -1
  2. -2
  3. -1.5
  4. 1

What does max({3, 9, 4}) return?

  1. 3
  2. 4
  3. 9
  4. It does not compile