C++ pow()

The pow() function computes a base number raised to the power of exponent number.

This function is defined in <cmath> header file.


[Mathematics] baseexponent = pow(base, exponent) [C++ Programming]

pow() Prototype [As of C++ 11 standard]

double pow(double base, double exponent);
float pow(float base, float exponent);
long double pow(long double base, long double exponent);
Promoted pow(Type1 base, Type2 exponent); // For other argument types

Since C++11, if any argument passed to pow() is long double, the return type Promoted is long double. If not, the return type Promoted is double.


pow() Parameters

The pow() function takes two arguments:

  • base - the base value
  • exponent - exponent of the base

pow() Return Value

The pow() function returns base raised to the power of exponent.


Example 1: How pow() works in C++?

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	double base, exponent, result;
	
	base = 3.4;
	exponent = 4.4;
	result = pow(base, exponent);
	
	cout << base << "^" << exponent << " = " << result;
	
	return 0;
}

When you run the program, the output will be:

3.4^4.4 = 218.025

Example 2: pow() With Different Combination of Arguments

#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
	long double base = 4.4, result;
	int exponent = -3;
	result = pow(base, exponent);
	cout << base << "^" << exponent << " = " << result << endl;
	
	
	// Both arguments int
	// pow() returns double in this case
	int intBase = -4, intExponent = 6;
	double answer;
	answer = pow(intBase, intExponent);
	cout << intBase << "^" << intExponent << " = " << answer;
	
	return 0;
}

When you run the program, the output will be:

4.4^-3 = 0.0117393
-4^6 = 4096