Quick Check

From the signatures, we can tell that they are different:

pow(x, y[, z])

math.pow(x, y)

Also, trying it in the shell will give you a quick idea:

>>> pow is math.pow
False

Testing the differences

Another way to understand the differences in behaviour between the two functions is to test for them:

import math
import traceback
import sys

inf = float("inf")
NaN = float("nan")

vals = [inf, NaN, 0.0, 1.0, 2.2, -1.0, -0.0, -2.2, -inf, 1, 0, 2]

tests = set([])

for vala in vals:
  for valb in vals:
    tests.add( (vala, valb) )
    tests.add( (valb, vala) )


for a,b in tests:
  print("math.pow(%f,%f)"%(a,b) )
  try:
    print("    %f "%math.pow(a,b))
  except:
    traceback.print_exc()
  
  print("__builtins__.pow(%f,%f)"%(a,b) )
  try:
    print("    %f "%__builtins__.pow(a,b))
  except:
    traceback.print_exc()

We can then notice some subtle differences. For example:

math.pow(0.000000,-2.200000)
    ValueError: math domain error

__builtins__.pow(0.000000,-2.200000)
    ZeroDivisionError: 0.0 cannot be raised to a negative power

There are other differences, and the test list above is not complete (no long numbers, no complex, etc...), but this will give us a pragmatic list of how the two functions behave differently. I would also recommend extending the above test to check for the type that each function returns. You could probably write something similar that creates a report of the differences between the two functions.

math.pow()

math.pow() handles its arguments very differently from the builtin ** or pow(). This comes at the cost of flexibility. Having a look at the source, we can see that the arguments to math.pow() are cast directly to doubles:

static PyObject *
math_pow(PyObject *self, PyObject *args)
{
    PyObject *ox, *oy;
    double r, x, y;
    int odd_y;

    if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy))
        return NULL;
    x = PyFloat_AsDouble(ox);
    y = PyFloat_AsDouble(oy);
/*...*/

The checks are then carried out against the doubles for validity, and then the result is passed to the underlying C math library.

builtin pow()

The built-in pow() (same as the ** operator) on the other hand behaves very differently, it actually uses the Objects's own implementation of the ** operator, which can be overridden by the end user if need be by replacing a number's __pow__(), __rpow__() or __ipow__(), method.

For built-in types, it is instructive to study the difference between the power function implemented for two numeric types, for example, floats, long and complex.

Overriding the default behaviour

Emulating numeric types is described here. essentially, if you are creating a new type for numbers with uncertainty, what you will have to do is provide the __pow__(), __rpow__() and possibly __ipow__() methods for your type. This will allow your numbers to be used with the operator:

class Uncertain:
  def __init__(self, x, delta=0):
    self.delta = delta
    self.x = x
  def __pow__(self, other):
    return Uncertain(
      self.x**other.x, 
      Uncertain._propagate_power(self, other)
    )
  @staticmethod
  def _propagate_power(A, B):
    return math.sqrt(
      ((B.x*(A.x**(B.x-1)))**2)*A.delta*A.delta +
      (((A.x**B.x)*math.log(B.x))**2)*B.delta*B.delta
    )

In order to override math.pow() you will have to monkey patch it to support your new type:

def new_pow(a,b):
    _a = Uncertain(a)
    _b = Uncertain(b)
    return _a ** _b

math.pow = new_pow

Note that for this to work you'll have to wrangle the Uncertain class to cope with an Uncertain instance as an input to __init__()

Answer from brice on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_math_pow.asp
Python math.pow() Method
The math.pow() method returns the value of x raised to power y.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
2 weeks ago - Return e raised to the power x, minus 1. Here e is the base of natural logarithms. For small floats x, the subtraction in exp(x) - 1 can result in a significant loss of precision; the expm1() function provides a way to compute this quantity ...
Discussions

Difference between the built-in pow() and math.pow() for floats, in Python? - Stack Overflow
Is there a difference in the results returned by Python's built-in pow(x, y) (no third argument) and the values returned by math.pow(), in the case of two float arguments. I am asking this question More on stackoverflow.com
🌐 stackoverflow.com
Why does my math.pow function in Python not work?
It's giving you an OverflowError, which probably means that you've got some number in there that's way too big to represent. What inputs (n and a) did you run your code with to get that error? More on reddit.com
🌐 r/learnprogramming
5
5
March 6, 2018
Is there a difference?
You could always try it out. For example: >>> import timeit >>> timeit.timeit(lambda: math.pow(10000,20)) 1.561967187000846 >>> timeit.timeit(lambda: pow(10000, 20)) 3.4198536449985113 >>> timeit.timeit(lambda: 10000**20) 3.278008331995806 Feel free to modify the sizes to ehatever you're dealing with. More on reddit.com
🌐 r/learnpython
24
24
May 16, 2022
Taking the positive square root: math.sqrt, math.pow and **
You could try writing some python to iterate over values of x comparing the execution times and returned results from each of the three methods. I would expect some variation in time for each method but no difference in calculated value. More on reddit.com
🌐 r/learnpython
2
2
January 7, 2021
🌐
Unstop
unstop.com › home › blog › python pow() function | syntax, exceptions & code examples
Python pow() Function | Syntax, Exceptions & Code Examples
February 17, 2025 - The Python pow() function is an inbuilt function for calculating the power of a number. It takes a base number and an exponent and returns the result of the base raised to the exponent.
🌐
GeeksforGeeks
geeksforgeeks.org › python › math-pow-in-python
math.pow() in Python - GeeksforGeeks
July 23, 2025 - In Python, math.pow() is a function that helps you calculate the power of a number. It takes two numbers as input: the base and the exponent.
🌐
W3Schools
w3schools.com › python › python_math.asp
Python Math
The min() and max() functions can ... (positive) value of the specified number: ... The pow(x, y) function returns the value of x to the power of y (xy)....
Top answer
1 of 4
60

Quick Check

From the signatures, we can tell that they are different:

pow(x, y[, z])

math.pow(x, y)

Also, trying it in the shell will give you a quick idea:

>>> pow is math.pow
False

Testing the differences

Another way to understand the differences in behaviour between the two functions is to test for them:

import math
import traceback
import sys

inf = float("inf")
NaN = float("nan")

vals = [inf, NaN, 0.0, 1.0, 2.2, -1.0, -0.0, -2.2, -inf, 1, 0, 2]

tests = set([])

for vala in vals:
  for valb in vals:
    tests.add( (vala, valb) )
    tests.add( (valb, vala) )


for a,b in tests:
  print("math.pow(%f,%f)"%(a,b) )
  try:
    print("    %f "%math.pow(a,b))
  except:
    traceback.print_exc()
  
  print("__builtins__.pow(%f,%f)"%(a,b) )
  try:
    print("    %f "%__builtins__.pow(a,b))
  except:
    traceback.print_exc()

We can then notice some subtle differences. For example:

math.pow(0.000000,-2.200000)
    ValueError: math domain error

__builtins__.pow(0.000000,-2.200000)
    ZeroDivisionError: 0.0 cannot be raised to a negative power

There are other differences, and the test list above is not complete (no long numbers, no complex, etc...), but this will give us a pragmatic list of how the two functions behave differently. I would also recommend extending the above test to check for the type that each function returns. You could probably write something similar that creates a report of the differences between the two functions.

math.pow()

math.pow() handles its arguments very differently from the builtin ** or pow(). This comes at the cost of flexibility. Having a look at the source, we can see that the arguments to math.pow() are cast directly to doubles:

static PyObject *
math_pow(PyObject *self, PyObject *args)
{
    PyObject *ox, *oy;
    double r, x, y;
    int odd_y;

    if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy))
        return NULL;
    x = PyFloat_AsDouble(ox);
    y = PyFloat_AsDouble(oy);
/*...*/

The checks are then carried out against the doubles for validity, and then the result is passed to the underlying C math library.

builtin pow()

The built-in pow() (same as the ** operator) on the other hand behaves very differently, it actually uses the Objects's own implementation of the ** operator, which can be overridden by the end user if need be by replacing a number's __pow__(), __rpow__() or __ipow__(), method.

For built-in types, it is instructive to study the difference between the power function implemented for two numeric types, for example, floats, long and complex.

Overriding the default behaviour

Emulating numeric types is described here. essentially, if you are creating a new type for numbers with uncertainty, what you will have to do is provide the __pow__(), __rpow__() and possibly __ipow__() methods for your type. This will allow your numbers to be used with the operator:

class Uncertain:
  def __init__(self, x, delta=0):
    self.delta = delta
    self.x = x
  def __pow__(self, other):
    return Uncertain(
      self.x**other.x, 
      Uncertain._propagate_power(self, other)
    )
  @staticmethod
  def _propagate_power(A, B):
    return math.sqrt(
      ((B.x*(A.x**(B.x-1)))**2)*A.delta*A.delta +
      (((A.x**B.x)*math.log(B.x))**2)*B.delta*B.delta
    )

In order to override math.pow() you will have to monkey patch it to support your new type:

def new_pow(a,b):
    _a = Uncertain(a)
    _b = Uncertain(b)
    return _a ** _b

math.pow = new_pow

Note that for this to work you'll have to wrangle the Uncertain class to cope with an Uncertain instance as an input to __init__()

2 of 4
43

math.pow() implicitly converts its arguments to float:

>>> from decimal import Decimal
>>> from fractions import Fraction
>>> math.pow(Fraction(1, 3), 2)
0.1111111111111111
>>> math.pow(Decimal(10), -1)
0.1

but the built-in pow does not:

>>> pow(Fraction(1, 3), 2)
Fraction(1, 9)
>>> pow(Decimal(10), -1)
Decimal('0.1')

My goal is to provide an implementation of both the built-in pow() and of math.pow() for numbers with uncertainty

You can overload pow and ** by defining __pow__ and __rpow__ methods for your class.

However, you can't overload math.pow (without hacks like math.pow = pow). You can make a class usable with math.pow by defining a __float__ conversion, but then you'll lose the uncertainty attached to your numbers.

Find elsewhere
🌐
Tutorial Gateway
tutorialgateway.org › python-pow
Python pow Function
March 31, 2025 - The Python pow function is used to calculate the Power of the specified expression and the syntax it is ... Base: Please specify the base value here. Exponent: Please specify the Exponent value or power here.
🌐
Udacity
udacity.com › blog › 2024 › 11 › understanding-exponents-in-python-the-power-of-the-operator-and-math-pow.html
Understanding Exponents in Python: The Power of the ** Operator and math.pow() | Udacity
November 26, 2024 - Python’s math module provides a pow() function for exponentiation, which can seem interchangeable with the ** operator at first glance.
🌐
Code.mu
code.mu › en › python › manual › math › pow
The pow method of math module - power of number in Python
The pow method of the math module returns a number raised to a power. In the first parameter of the method, we specify the number we need, in the second parameter - to what power we want to raise it.
🌐
Tutorialspoint
tutorialspoint.com › home › python › python pow() function
Python math.pow() Method
February 21, 2009 - Learn how to use the pow() function in Python to compute the power of a number with examples and detailed explanations.
🌐
Interactive Chaos
interactivechaos.com › en › python › function › mathpow
math.pow | Interactive Chaos
February 1, 2021 - Python scenarios · Full name · math.pow · Library · math · Syntax · math.pow(x, y) Description · The math.pow function returns the result of raising x to y. Unlike the built-in function pow or the ** operator, math.pow always returns a real number, even if x and y are integers.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pow-function
pow() Function - Python - GeeksforGeeks
April 14, 2025 - pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result.
🌐
Pythontic
pythontic.com › modules › math › pow
The pow() function - Python math module | Pythontic.com
The po() function in Python math module returns the value of a number raised to the specified power. In other words, it multiplies the number the power times.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › python power operator and function
Python Power Operator and Function | phoenixNAP KB
December 16, 2025 - The function outputs the operation result of the base value to the power of the exponent value. Note: Python packages, such as NumPy and SciPy, enable performing advanced math calculations.
🌐
DataCamp
datacamp.com › tutorial › exponents-in-python
Exponents in Python: A Comprehensive Guide for Beginners | DataCamp
November 25, 2024 - You can use them for many practical scenarios, such as finding compound interest, modeling population growth, and more. Double-asterisk operator (**): Simple and direct for everyday use. pow(): Versatile with an optional "modulus" trick. math.pow(): Precise results as floating-point numbers.
🌐
Quora
quora.com › What-is-the-code-behind-the-math-pow-function
What is the code behind the math.pow function? - Quora
Answer (1 of 4): [code]Public static int pow(int num, int pow) { int ans =num; if (pow ==1) { return num; } else if (i==0) { return 1; } for(int i =1, i!= pow, i++) { ans = ans*num; } return ans; } // I think this is it. This code does not deal // with negative exponents or frac...
🌐
Initial Commit
initialcommit.com › blog › pow-function-java-python
Pow() Function in Java and Python
October 19, 2021 - Because Python supports implicit typecasting, we don't need to worry about defining the variable types as we did in Java. Instead, the pow() function accepts any numerical value as an argument. Here, we'll write a similar script in Python to calculate the value of 34: import math base = 3 exponent = 4 raised_value = math.pow(base, exponent) print(raised_value)