10

I was just bitten by the following scenario:

>>> -1 ** 2
-1

Now, digging through the Python docs, it's clear that this is intended behavior, but why? I don't work with any other languages with power as a builtin operator, but not having unary negation bind as tightly as possible seems dangerously counter-intuitive to me.

Is there a reason it was done this way? Do other languages with power operators behave similarly?

4
  • 2
    wolframalpha.com/input/?i=-1^2 yields -1 It's the same in Mathematica Jun 1, 2009 at 21:42
  • 5
    That's why I don't like infix notation. (expt -1 2) is unambiguous, as is (- (expt 1 2)).
    – Svante
    Jun 1, 2009 at 22:19
  • It's definitely counter-intuitive: not if the minus sign was inbetween two operands, but as a unary operater it should take precedence (but it never does, just one of those strange things). Jun 1, 2009 at 22:50
  • @Svante — That's why I've never run into this before, I suppose. In most languages, it's pow(-1, 2) or something similar.
    – Ben Blank
    Jun 1, 2009 at 23:05

5 Answers 5

23

That behaviour is the same as in math formulas, so I am not sure what the problem is, or why it is counter-intuitive. Can you explain where have you seen something different? "**" always bind more than "-": -x^2 is not the same as (-x)^2

Just use (-1) ** 2, exactly as you'd do in math.

8
  • 6
    @Jason: go read your pre-algebra book again... that's not how math works.
    – rmeador
    Jun 1, 2009 at 21:43
  • 8
    @Jason, you may disagree but that's the standard that mathematicians have settled on. Jun 1, 2009 at 21:44
  • 1
    I agree that -x^2 should be -(x^2), but for constants, I feel like -1^2 should parse as (-1)^2. Unfortunately, -1 is not parsed as a single token of a constant -1, but rather as the unary negation operator on the constant 1. Jun 1, 2009 at 21:47
  • 1
    there's a shift-reduce conflict that you chose how to solve, depending on your rule to resolve conflict, you may find counter-intuitive or not...
    – LB40
    Jun 1, 2009 at 21:50
  • 1
    for the same reason 10-8/2 = 6 and not 1, in fact in math you see it like 10-(8/2) and not like (10-8)/2. In your case power priority is higher than sum (or sub) priority and python (correctly) read -12 as -(12) and not as (-1)**2 Jun 1, 2009 at 22:23
5

Short answer: it's the standard way precedence works in math.

Let's say I want to evaluate the polynomial 3x3 - x2 + 5.

def polynomial(x):
    return 3*x**3 - x**2 + 5

It looks better than...

def polynomial
    return 3*x**3 - (x**2) + 5

And the first way is the way mathematicians do it. Other languages with exponentiation work the same way. Note that the negation operator also binds more loosely than multiplication, so

-x*y === -(x*y)

Which is also the way they do it in math.

3
  • 1
    The binary subtraction example seems irrelevant. The unary minus example with multiplication is more valuable.
    – S.Lott
    Jun 2, 2009 at 1:23
  • 1
    -(x*y) is in fact the same as (-x)*y, so whether negation binds more or less loosely than multiplication isn't really clear from this (although I agree with you). Also, @S.Lott, I'm not sure math distinguishes between unary and binary -...
    – weronika
    Dec 7, 2012 at 5:32
  • 1
    @weronika: They may be the same mathematically, but they're not necessarily the same in a computer program. For example, if you are working with 32-bit integers, then (-0x40000000) * 2 is not the same as -(0x40000000 * 2), because the latter causes overflow. If your environment traps overflow, then only the first one is correct. This can apply to Python, too, if you are manipulating 32-bit integers e.g. through numpy. Dec 7, 2012 at 5:42
3

If I had to guess, it would be because having an exponentiation operator allows programmers to easily raise numbers to fractional powers. Negative numbers raised to fractional powers end up with an imaginary component (usually), so that can be avoided by binding ** more tightly than unary -. Most languages don't like imaginary numbers.

Ultimately, of course, it's just a convention - and to make your code readable by yourself and others down the line, you'll probably want to explicitly group your (-1) so no one else gets caught by the same trap :) Good luck!

1

It seems intuitive to me.

Fist, because it's consistent with mathematical notaiton: -2^2 = -4.

Second, the operator ** was widely introduced by FORTRAN long time ago. In FORTRAN, -2**2 is -4, as well.

-1

Ocaml doesn't do the same

# -12.0**2.0
  ;;
- : float = 144.

That's kind of weird...

# -12.0**0.5;;
- : float = nan

Look at that link though... order of operations

1
  • 3
    Ocaml is broken. It doesn't follow this basic math rule.
    – nosklo
    Jun 1, 2009 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.