pow() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The pow() function is used to calculate the power of any given number.

Syntax for pow() Function in C

The syntax of the pow() function is as follows:

Parameters for pow() in C

The pow() function takes two integer arguments x and y.

  1. x: The variable x represents the base value, whose power needs to be calculated. It is of the type double.
  2. y: The variable y represents the exponent value. It is of the type double.

Return Values for pow() Function in C

The pow() function returns the value xyx^y (x raised to the power y) where x and y are two variables of type double.

The return type is double.

Example for pow() in C

Output:

In the above example, we calculated the value of 2.532.5^3 using the pow() function in C.

What is pow() in C?

The pow() function (power function) in C is used to find the value xyx^y (x raised to the power y) where x is the base and y is the exponent. Both x and y are variables of the type double. The value returned by pow() is of the type double.

The pow() function is a predefined function present in the math.h header file. In order to use this function, we need to include the math.h header file in our program.

How to Use the pow() Function in C?

To use pow(), we need to include the math.h library in our program. The pow() function takes two arguments - the base value (x) and the exponent value (y).

The value xyx^y can be calculated using pow(x, y).

Working of pow() Function in C

The pow() function takes values of type double as its arguments and returns a value of type double. But the function does not always work with integers.

For example, if the value returned by pow(4, 3) is assigned to an integer variable, the output might be 63 on some compilers and 64 on other compilers. This happens because the value of 434^3 (i.e. 64) could be stored as 63.999999 in some compilers and 64.0000000001 in others. When these values are assigned to int, 63.999999 converts to 63 and hence we see an incorrect output.

We can overcome this problem by adding 0.0000000001 (191^{-9} or 1e-9) to the output and explicitly typecast it into int. For example, (int)(pow(4, 3) + 1e-9) will always give the correct answer.

Few More Examples

Example 1: Base of type int and power of type double

Output:

Explanation: In the above example, the base was of type int while the power was of type double. We used pow() on these variables to get our output.

Example 2: Output stored in an int variable

Output:

Explanation: In the above example, we had to store the value returned by pow() in an int variable. So, we added 1e-9 to pow(4, 3) and then typecasted the sum into int. By doing so, we made sure that the answer will be correct.

Example 3: Calculating Square Root Using pow()

Output:

Explanation: pow() can be used to calculate the roots of positive numbers. If we calculate the root of a negative number using pow(), we will get -nan as the output which means it is an undefined value as root of negative number is not a real number.

Example 4: Using negative values with power of type int

Output:

Explanation: The power's value in pow() can be negative.

Example 5: Using negative values with power of type double

Output:

Explanation: If the base is negative and the power is of type double, the output will be -nan as it is an undefined entity.

Conclusion

  • The pow() function calculates the power of any given number.
  • The arguments and the output of pow() are of type double.
  • pow(x,y) will return xyx^y.
  • It can be used to calculate roots and fractions of numbers.