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
Call
What 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
Parameter
Description
x
Required. The base, the number that is multiplied.
y
Required. The exponent, the power to raise the base to. It can be negative or have a decimal part.
Return Value
Return Value
Description
x to the power y
The result of xy in the promoted type.
1
pow(x, 0) is always 1, and pow(0, 0) is 1 too.
NaN
A negative x with a non-whole y, such as pow(-8, 0.5).
inf
The 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.
By the standard, pow(0, 0) returns 1. Any number to the power of zero is one.
A negative base with a whole exponent is fine. (-2) * (-2) * (-2) is -8.
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.