Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Odd pow Behavior
#1
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#2
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020