C++ std::pow() Function

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

Definition

In C++, std::pow(x, y) calculates x to the power of y. The first number is the base and the second is the exponent. pow(2.0, 10.0) multiplies 2 by itself ten times and returns 1024.

The exponent does not have to be a whole number. pow(9.0, 0.5) is the same as the square root of 9, which is 3. A negative exponent gives a fraction, so pow(2.0, -2.0) is 0.25. The result is a double, even if both inputs look like whole numbers.

std::pow() is declared in <cmath> and is part of the group Roots and powers. These functions raise numbers to powers or take roots. Watch the domain: a negative number has no real square root, so the answer is NaN.

Syntax

Include <cmath> and call std::pow. Start with these lines.

C++
double a = std::pow(2.0, 10.0);   // 1024
double b = std::pow(9.0, 0.5);    // 3
float  c = std::pow(2.0f, 3.0f);  // 8.0f
CallWhat it does
std::pow(2.0, 10.0)Base 2, exponent 10. Returns 1024.
std::pow(2.0f, 3.0f)Both arguments are float, so the float overload returns a float.
std::pow(2, 10)Two integers are converted to double. Returns 1024 as a double.

Write std::pow so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       pow(float x, float y);
double      pow(double x, double y);
long double pow(long double x, long double y);
// plus overloads for mixed and integer arguments

Parameters

ParameterDescription
xRequired. The base, the number that is multiplied.
yRequired. The exponent, the power to raise the base to. It can be negative or have a decimal part.

Return Value

Return ValueDescription
x to the power yThe result of xy in the promoted type.
1pow(x, 0) is always 1, and pow(0, 0) is 1 too.
NaNA negative x with a non-whole y, such as pow(-8, 0.5).
infThe result is too big for a double, or pow(0, -1).

C++ Examples

Example 1 — Basic Powers

Raise numbers to whole, fractional, and negative powers.

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

int main() {
    std::cout << "pow(2, 10)  = " << std::pow(2.0, 10.0) << '\n';
    std::cout << "pow(5, 3)   = " << std::pow(5.0, 3.0) << '\n';
    std::cout << "pow(9, 0.5) = " << std::pow(9.0, 0.5) << '\n';
    std::cout << "pow(2, -2)  = " << std::pow(2.0, -2.0) << '\n';
    return 0;
}

How It Works

  1. pow(2, 10) is 2 multiplied ten times, which is 1024. pow(5, 3) is 5 * 5 * 5, or 125.
  2. An exponent of 0.5 means a square root, so pow(9, 0.5) gives 3.
  3. A negative exponent flips the number: 2-2 is 1 / (2 * 2), which is 0.25.

Example 2 — Compound Interest

A bank adds 4 percent each year. After n years the money is start * pow(1.04, n).

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

int main() {
    double start = 2000.0;
    double rate = 1.04;

    std::cout << std::fixed << std::setprecision(2);
    for (int years : {1, 5, 10}) {
        std::cout << "After " << years << " years: " << start * std::pow(rate, years) << '\n';
    }
    return 0;
}

How It Works

  1. A growth of 4 percent a year is the factor 1.04. Each year multiplies the money by this factor once.
  2. std::pow(rate, years) applies the factor many times in one call. The integer years is converted to a double.
  3. std::fixed and std::setprecision(2) print two decimals, like money. After 10 years the 2000 has grown to 2960.49.

Example 3 — Edge Cases: Zero, Negative Base, and Overflow

Some inputs have no simple answer. See what pow() returns for them.

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

int main() {
    std::cout << "pow(0, 0)  = " << std::pow(0.0, 0.0) << '\n';
    std::cout << "pow(-2, 3) = " << std::pow(-2.0, 3.0) << '\n';
    std::cout << "pow(0, -1) = " << std::pow(0.0, -1.0) << '\n';

    double r = std::pow(-8.0, 0.5);
    std::cout << "pow(-8, 0.5) is NaN: " << std::isnan(r) << '\n';
    return 0;
}

How It Works

  1. By the standard, pow(0, 0) returns 1. Any number to the power of zero is one.
  2. A negative base with a whole exponent is fine. (-2) * (-2) * (-2) is -8.
  3. pow(0, -1) would be one divided by zero, so it returns inf. A negative base with 0.5 means the root of a negative number, so the result is NaN.

Frequently Asked Questions

std::pow(x, y) raises x to the power y. For example, pow(2.0, 10.0) is 1024, because 2 is multiplied by itself 10 times.
x raised to the power y, as a double (or float or long double). pow(0, 0) is 1, and a negative base with a non-whole exponent gives NaN.
Yes, but x * x is simpler, faster, and exact. Use pow() when the exponent is a variable, or when it is not a whole number.
pow() works with doubles, and the result can be a hair below 100, such as 99.9999999. Casting to int cuts off the decimals. Use std::lround(std::pow(10, 2)) or a loop for exact integer powers.
Include <cmath> and write std::pow(). That always works and picks the C++ overloads. The global name ::pow often works too, but the standard does not promise it, and it may only take a double.

Next: remainder()

Keep going through the <cmath> functions. IEEE remainder: x minus the nearest multiple of y.

remainder() tutorial →

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