C++ Math

C++ math means using operators and library functions to calculate numeric values. Basic arithmetic uses operators such as + and *, while more advanced operations come from the <cmath> header.

You have already seen numbers and operators. This lesson shows how to combine them with common math functions in beginner-friendly programs.

Basic Arithmetic

C++ supports the usual arithmetic operators for numbers. Addition, subtraction, multiplication, and division work with numeric variables and literal values.

Operator Meaning Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Remainder a % b
#include <iostream>

int main(void) {
    int apples = 17;
    int boxes = 5;

    std::cout << "Total apples: " << apples << std::endl;
    std::cout << "Full boxes: " << apples / boxes << std::endl;
    std::cout << "Left over: " << apples % boxes << std::endl;

    return 0;
}

Output:

Total apples: 17
Full boxes: 3
Left over: 2

Because apples and boxes are both int values, apples / boxes performs integer division. The fractional part is discarded, so 17 / 5 becomes 3. The remainder operator % gives the leftover amount.

The cmath Header

For functions such as square root, powers, and rounding, include <cmath>. These functions are called with parentheses, such as std::sqrt(64.0).

#include <cmath>
#include <iostream>

int main(void) {
    double side = 9.0;
    double value = 7.6;

    std::cout << "Square root: " << std::sqrt(side) << std::endl;
    std::cout << "Power: " << std::pow(3.0, 4.0) << std::endl;
    std::cout << "Round: " << std::round(value) << std::endl;
    std::cout << "Floor: " << std::floor(value) << std::endl;
    std::cout << "Ceil: " << std::ceil(value) << std::endl;

    return 0;
}

Output:

Square root: 3
Power: 81
Round: 8
Floor: 7
Ceil: 8

std::sqrt() returns the square root. std::pow(base, exponent) raises one number to a power. std::round() rounds to the nearest whole number, std::floor() rounds down, and std::ceil() rounds up.

Absolute Values

An absolute value is the distance from zero, so it is never negative. Use std::abs() when you need the positive version of a number.

#include <cmath>
#include <iostream>

int main(void) {
    int temperatureChange = -12;
    double accountChange = -45.75;

    std::cout << "Temperature change: " << std::abs(temperatureChange) << std::endl;
    std::cout << "Account change: " << std::abs(accountChange) << std::endl;

    return 0;
}

Output:

Temperature change: 12
Account change: 45.75

Integer And Floating-Point Results

Choose numeric types based on the result you need. Use int for whole-number counts and double for values that may contain decimals.

#include <iostream>

int main(void) {
    int points = 7;
    int players = 2;

    std::cout << "Integer average: " << points / players << std::endl;
    std::cout << "Decimal average: " << static_cast<double>(points) / players << std::endl;

    return 0;
}

Output:

Integer average: 3
Decimal average: 3.5

The cast static_cast<double>(points) converts one value before division, so C++ performs floating-point division instead of integer division.

Common Mistakes

  • Forgetting to include <cmath> before using functions such as std::sqrt() or std::pow().
  • Expecting integer division to keep decimals.
  • Using ^ for powers. In C++, use std::pow(); ^ is a different operator.
  • Using % with decimal values. The remainder operator is for integer operands.

Takeaway: use basic operators for simple arithmetic, and include <cmath> when you need common math functions like roots, powers, rounding, and absolute values.