Python Math pow() function calculates the value of the number raised to the power
of another number. This function returns the float value of x raised to power y.
math.pow(x, y)
Note: This method converts both arguments into a float and If x is negative and y is not an integer, it returns a ValueError.
Math pow in Python example
A simple example code gets the value of the base number raised to the exponent number.
import math
# positive: baseNumber positive: exponent
print("pow(2,3): ", math.pow(2, 3))
# positive: baseNumber negative: exponent
print("pow(0.25,-2): ", math.pow(0.25, -2))
# negative: baseNumber positive: exponent
print("pow(-10,2): ", math.pow(-10, 2))
# negative: baseNumber negative: exponent
print("(-0.5,-1) :", math.pow(-0.5, -1))
Output:
Difference between the built-in pow() and math.pow() for floats, in Python?
Answer: From the signatures, we can tell that they are different:
pow(x, y[, z])
math.pow(x, y)
The return value of pow() can be an integer, floating point, or even a complex number. The return value of math. pow() is always a floating point number.
Differences between inbuilt pow() function and math.pow() function.
pow() | math.pow() |
---|---|
pow() is an inbuilt function. | math.pow() is imported from the math module. |
It accepts an optional third argument (modulus). | It doesn’t accept a third argument. |
It accepts an integer, floating point, and complex numbers as base or exponent. | It accepts an integer and floating point numbers. |
It doesn’t change its arguments. | It changes its arguments to floating point numbers. |
The return value of pow() can be an integer, floating point, or even a complex number. | The return value of math.pow() is always a floating point number. |
Do comment if you have any doubts or suggestions on this Python math module topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.