Power Function in C++

Learn via video courses
Topics Covered

Overview

In C++, we have a set of inbuilt functions in the <cmath> header file that helps us perform various mathematical operations, such as finding the square root of a number, returning the power of a number, etc. In this article, we will explore the function that helps us to calculate the exponent of a given number x raised to another number y. This function is known as the Power Function in C++.

Introduction

The power of a number is the result obtained if we multiply the number by itself for x number of times, where x is the exponent to which the number is to be raised. Suppose we are given base=x and exponent=n then power in C++ returns xnx^n (x * x * x * . . . . . . . n number of times). The syntax of power in C++ is written as pow(x,n).

syntax of power in C++

Example:

We can calculate a number's power manually or use the pow function. For calculating manually, we can keep a variable called the answer and initialize it as one. We can then use a while loop to multiply the answer with the base exponent number of times.

However, this works only when the exponent is a positive integer. To calculate the power of a number raised to any real number, we use the pow() function in C++, defined in the <cmath> header file.

The function takes in two arguments, one is the base, and the other is the exponent. The function returns the value of the base raised to the power exponent.

In C++, pow(x, y) = xyx^y.

Be sure to include the <cmath> header file in your program whenever you use the pow() function.

Example :

Output:

Explanation: pow(2.0, 4.0) computes 2.0 raised to the power 4.0, the result of which is 16.

Syntax

The syntax of the power function in C++ is as follows:

Keep in mind that the data type of base and exponent should be double. If we pass an integer, the function will convert it into a double data type. But sometimes, it might lead to incorrect results.

Parameters

There are two parameters to the pow() function:

  • base: This is the base value whose power is to be calculated.
  • exponent: This is the exponent to which the base is to be raised.

pow() Return Value

The return value of the Power function in C++ is the result of the base raised to the power exponent. The return value is of data type double.

  • If our base is zero, the return value of the pow() function will be 0.0 since zero raised to the power of any number gives zero.
  • If our exponent is zero, the return value of the pow() function will be 1.0 since any number raised to the power of zero gives one.

pow() Prototypes

Prototypes are used to tell the compiler about the data types of the arguments, the number of arguments passed to a function and the function's return value. Let us see the prototypes of the pow() function in C++.

  • double pow(double x, double y) - If the base is of data type double and the exponent is of data type double, the return value of the pow function will be of data type double.
  • long double pow(long double x, long double y)- If the base is of data type long double and the exponent is of data type long double, the return value of the pow function will be of data type long double.
  • float pow(float x, float y) - If the base is of data type float and the exponent is of data type float, the return value of the pow function will be of data type float.

Working of pow() Function with Integers

As we studied earlier, if integer values are passed to the pow function, they are converted to double data type, and the function returns a double value. But this might lead to incorrect values sometimes, though very rarely. Let us see why.

Suppose we are to calculate 6 raised to power 2. We pass integer 6 and integer 2 to the pow function. Now, if 6 is converted to its lower double value by the compiler, i.e., 5.99, the resultant answer will be 35.998800, which becomes 35. But the correct answer should have been 36.

To prevent this and get a more accurate answer, we can add 1e-9 to our answer and typecast it to int. Let us see the code for the same.

Output:

This way, the pow() function can be used with integers to get an accurate result.

pow() With Different Arguments

Let us see the working of the pow() function with different arguments (double, float, and integer arguments) through the following program.

Output:

The above program is used to demonstrate the working of the pow function with different arguments.

  • In the first example, the data type of our base is long double, and the data type of our exponent is an integer.
  • In the second example, the data type of our base is int, and the exponent is a positive integer.
  • In the third example, the data type of our base is int, and the exponent is a negative integer.
  • In the fourth example, the data type of our base and exponent is float.

Thus we saw how the pow() function calculates the power for arguments of different data types.

Let us go through some special cases that can arise while using the pow function:

  • If Base is Finite Negative and Exponent is Finite but not Integer, it’s an exception in the pow in C++; therefore, we get nan (not a number) as our output.
  • If Base is 0 and Exponent is negative, it means we are doing 1/0 * 1/0 * 1/0 . . . exponent number of times. Since 1/0` is infinity, we get infinity as our result.
  • When our result is too small or too large to be represented by a return type value, the result may overflow, and we may get infinity as an answer.

Conclusion

  • pow() function is used to calculate the value of a number x raised to the power y.
  • The pow() function takes in double arguments and returns the answer of data type double.
  • The pow() function has prototypes for double, float, and long double.
  • Integer arguments are converted to double data types by the pow() function, which can lead to incorrect results.