English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ pow() function usage and example

C++  Library Function <cmath>

pow() function

This function calculates the power.<cmath>Header file definition.

[Math] base^exponent = pow(base, exponent) [C++ Language]

pow() prototype[From C ++ 11Standard begins]

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 parameter types

From C ++ 11Starting from C, if the parameters passed to pow() are of type long double, then the returned type Promoted is long double. If not, then the returned type Promoted is double.

pow() parameters

The pow() function takes two parameters:

  • base -The base

  • exponent -The exponent of the base

pow() return value

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

Example1:How pow() works in C ++How does it work in

#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 the program is run, the output is:

3.4^4.4 = 218.025

Example2:Different parameter combinations of pow()

#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 parameters are int
      // pow() returns double in this example
	int intBase = -4, intExponent = 6;
	double answer;
	answer = pow(intBase, intExponent);
	cout << intBase << "^" << intExponent << " = " << answer;
	return 0;
}

When the program is run, the output is:

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

C++  Library Function <cmath>