Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:22:23+00:00 2026-06-02T08:22:23+00:00

Is there a difference in the results returned by Python’s built-in pow(x, y) (no

  • 0

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 because the documentation for math.pow() implies that pow(x, y) (i.e. x**y) is essentially the same as math.pow(x, y):

math.pow(x, y)

Return x raised to the power y. Exceptional cases
follow Annex ‘F’ of the C99 standard as far as possible. In
particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x
is a zero or a NaN. If both x and y are finite, x is negative, and y
is not an integer then pow(x, y) is undefined, and raises ValueError.

Changed in version 2.6: The outcome of 1**nan and nan**0 was undefined.

Note the last line: the documentation implies that the behavior of math.pow() is that of the exponentiation operator ** (and therefore of pow(x, y)). Is this officially guaranteed?

Background: My goal is to provide an implementation of both the built-in pow() and of math.pow() for numbers with uncertainty that behaves in the same way as with regular Python floats (same numerical results, same exceptions, same results for corner cases, etc.). I have already implemented something that works quite well, but there are some corner cases that need to be handled.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:22:24+00:00Added an answer on June 2, 2026 at 8:22 am

    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__()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many question posted about getting the difference between two dates in Oracle.
Is there any difference between these two functions? I mean in-terms of the result
I'm passing some simple IDL code to Python. However the returned FFT values form
Is there difference in behavior between a constructor call and a procedure call in
is there a difference between a struct in c++ and a struct in c#?
Is there any difference in using login_required decorator in urls.py and in views.py ?
Is there any difference or associated risk opening with the following PHP variations? <?
Is there any difference between these tow pieces of code & which approach is
Is there a difference between ++x and x++ in java?
Is there a difference in double size when I run my app on 32

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.