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.
Quick Check
From the signatures, we can tell that they are different:
Also, trying it in the shell will give you a quick idea:
Testing the differences
Another way to understand the differences in behaviour between the two functions is to test for them:
We can then notice some subtle differences. For example:
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**orpow(). This comes at the cost of flexibility. Having a look at the source, we can see that the arguments tomath.pow()are cast directly to doubles: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:In order to override
math.pow()you will have to monkey patch it to support your new type:Note that for this to work you’ll have to wrangle the
Uncertainclass to cope with anUncertaininstance as an input to__init__()