C++ pow()
Published Oct 31, 2022Updated Dec 21, 2022
Contribute to Docs
The pow() function returns the result of a base value raised to the power of an exponent.
Syntax
pow(base, exponent)
The base and exponent parameters are integers or of type float. If base is zero, the function returns 0.0. If exponent is zero, the function returns 1.0.
- A domain error may occur if the
baseis a float andexponentis not an integer value. - A pole or range error may occur due to overflow.
- A range error may occur due to underflow.
Example
The following example uses the pow() function to raise 2 to the power of 2:
#include <iostream>#include <cmath>int main() {double base = 2;double exponent = 2;double result = pow(base, exponent);std::cout << base << " raised to the power of " << exponent << " equals " << result << ".\n";// Output: 2 raised to the power of 2 equals 4}
Codebyte Example
The following example is runnable and uses the pow() function to find the nth-root of 16 by raising the base to the reciprocal of the desired root:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours