0

Python seems to have trouble returning the correct value for numbers to the power of zero. When I give it a literal equation, it works properly, but it always returns positive 1 for anything more complex than a raw number to the zeroeth.
Here are some tests:

>>> -40 ** 0  # this is the correct result
-1
>>> (0 - 40) ** 0  # you'd expect this to give the same thing, but...
1
>>> a = -40  # let's try something else...
>>> a ** 0
1
>>> int(-40) ** 0  # this oughtn't to change anything, yet...
1
>>> -66.6 ** 0  # raw floats are fine.
-1.0
>>> (0 - 66.6) ** 0.0  # ...until you try and do something with them.
1.0

UPDATE: pow() gives this result, too, so probably the first result is exceptional...

>>> pow(-60, 0)
1

Could it be some problem with signed integers? I need this for a trinary switch with values 1, -1, or 0, depending on whether an input is any positive or negative value, or zero. I could accomplish the same thing with something like:

if   val > 0: switch = 1
elif val < 0: switch = -1
else:         switch = 0

...and then using the variable switch for my purposes.
But that wouldn't answer the question I have about how Python deals with zero-powers.
(I will also accept that -40 ** 0 only returns -1 by accident (phenomenally), but I doubt this is the case...)

Augusta
  • 7,171
  • 5
  • 24
  • 39
  • 1
    Any nonzero integer raised to the power of `0` should return a `+1`. So `(0-40)**0` is correctly producing a `+1`. The reason `-40**0` returns a `-1` is because the `**` binds more strongly to `40` than the `-`. – eigenchris Dec 03 '14 at 20:53
  • I presume, then, that `a = -60; a ** 0 -> 1` is evaluated as `(-60) ** 0`? That would make sense to me. – Augusta Dec 03 '14 at 21:37
  • Shoot. I really wanted to cheat this one out somehow. Looks like I'll actually have to do a "good job" instead. T-T – Augusta Dec 04 '14 at 19:28
  • For the record, I found a solution to the problem I was trying to solve with this whole exponent affair: `cmp(val, 0)` works perfectly: 1 for values larger than zero, -1 if smaller, 0 for zero. – Augusta Dec 30 '14 at 05:12

3 Answers3

5

Python is correct and doing what you would expect it to do. It is a matter of order of operations. ANY number (negative or positive) to zeroth power is equal to 1. But keep in mind also that multiplication comes before subtraction in order of operations. So in more detail, what python sees is this: 1st case:

-40 ** 0 = -(40 ** 0) = -(1) = -1

2nd case:

(0 - 40) ** 0 = (-40) ** 0 = 1

In the 5th case as well it has to do with the parentheses

int(-40) ** 0 = (-40) ** 0 = 1
ivan
  • 154
  • 7
  • Aa, I see. Thanks for explaining that to me. I thought that the first example (that is, the one that gave me what I wanted) may have been exceptional, but had hoped it wasn't. I imagine that `a = -60; a ** 0 -> 1` would be evaluated as `(-60) ** 0`, as well, also resolving to +1... Hm... damn. – Augusta Dec 03 '14 at 21:40
1

Just stumpled upon this question, I don't get the syntax.

But I don't think that (-40)^0 = -40^0.

On the left side, the exponential is the last operation. This is why the left side should equal 1. On the right side, the minus sign is the last operation. This is why the result should be -1.

1

There is not any problem , here . every number with power 0 is 1 .

In python signs like -.+,... have less precedences to power (**) so when you put 0 - 40 inside the parenthesize you have (-1)**0 that is 1 but when you do -1**0 first you have 1**0 then -.

>>> (0-4)**0 == (-1)**0 == 1
>>> -1**0 == -(1**0) == -1
Mazdak
  • 105,000
  • 18
  • 159
  • 188