Python Forum
Odd pow Behavior - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Odd pow Behavior (/thread-13612.html)



Odd pow Behavior - ichabod801 - Oct-23-2018

I was doing some unit testing and came across something odd:

>>> -2 ** 4
-16
>>> pow(-2, 4)
16
>>> import operator
>>> operator.pow(-2, 4)
16
>>> import math
>>> math.pow(-2, 4)
16.0
All of the documentation says that the various pow functions are equivalent to x ** y. However, the function call changes the order of operations. The negation is carried out when the number is evaluated as a parameter, instead of after the ** operator. So the pow functions are really calculating (x) ** (y), which is slightly different.


RE: Odd pow Behavior - micseydel - Oct-23-2018

That doesn't seem odd to me. It's just operator precedence. I might agree though that the exact docs should be updated.

I was more surprised that math.pow has a float result for what could have been an int, whereas the previous results all stayed ints.