11

In Python

>>> i = 3  
>>> -i**4  
-81

Why is -i**4 not evaluated as (-i)**4, but as -(i**4)?

I suppose one could argue that raising to a power takes precedence over (implicit) multiplication of i with minus one (i.e. you should read -1*i**4). But where I learned math, -i**n with n even and i positive, should come out positive.

Roman C
  • 49,761
  • 33
  • 66
  • 176
RolfBly
  • 3,612
  • 5
  • 32
  • 46
  • its about priority :) – Hackaholic Dec 31 '14 at 21:49
  • 3
    You answered your own question. Exponentiation has higher priority than multiplication. If you learned that -1**2 = 1 in a mathematical context, you learned wrong. (It is true that some programming languages do this, but they are also wrong in that they don't maintain the mathematical convention.) – BrenBarn Dec 31 '14 at 22:04
  • 1
    it is apparent that the author is talking about a mathematical notation that does not use parentheses around the base number, and not about a specific language grammar. this is the notation that I learned as well. And in this notation, `-1^2` (using LaTex expression) is 1. – akonsu Dec 31 '14 at 22:13
  • I still would say that that is incorrect as mathematical notation (as do most sources I can find; see for instance [here](http://en.wikipedia.org/wiki/Order_of_operations) and [here](http://mathforum.org/library/drmath/view/53194.html)). However, this is straying a bit from the question as it relates to Python; programming languages sadly don't always conform to established notational standards. – BrenBarn Jan 01 '15 at 19:35
  • @BrenBarn Take a calculator, press 1, press the negative button, press the square button. What do you get? I get 1. I wonder if US notation is different than European, but never mind, I accept your point. – RolfBly Jan 02 '15 at 20:28
  • @RolfBly: On just about any calculator I've seen that has a square button, that will produce -1, because those are usually scientific-style calculators that handle the order of operations. It's true that some calculators don't know about order of operations, though: on a simple "four-function", calculator, entering `2 + 3 X 4` will produce 20. – BrenBarn Jan 02 '15 at 20:35
  • @brenbarn Try windows calculator. scientific mode. – RolfBly Jan 02 '15 at 21:48
  • @RolfBly: Sure, but my point is that order of operations is a concept that applies to *mathematical expressions*, not calculator buttons. When you enter that into Windows calculator, you can see it doing `sqr(-1)` --- that is, the -1 is considered to be in parentheses. A more apt choice would be [Wolfram Alpha](http://www.wolframalpha.com/input/?i=-1^2), which is designed to work with mathematical expressions as such. – BrenBarn Jan 02 '15 at 21:53
  • @brenbarn That's my point too. I read -x^2 as the square of a negative number (neg num * neg num = pos num). You read it as the negative square of a number. (num * num, but then with a minus in front). I'm currently checking on a Dutch math forum whether methods have changed (I'm a BsC in EE 1984), or whether they're different this side of the Atlantic. – RolfBly Jan 03 '15 at 20:11
  • @RolfBly: I'm curious to hear what they say. I would find it stunning if `-x^2` is interpreted as `(-x)^2` when the exponent is written using normal notation (i.e., a superscript rather than `^`). It would totally change how all polynomials have to be written. – BrenBarn Jan 03 '15 at 20:27
  • 1
    @Brenbarn I'm happy to inform you that you are right and I was wrong. Thank you for the learning experience! This is the literal answer (translated by yours truly) from Wiskundeforum.nl (wiskunde is Dutch for Mathematics): "There is an essential difference between -a² = -1∙a² and (-a)² = -a∙-a = a², whereby -a² represents the opposite of a² and (-a)² represents the square of the opposite of a." So there. – RolfBly Jan 04 '15 at 16:43

4 Answers4

15

The ** operator binds more tightly than the - operator does in Python. If you want to override that, you'd use parentheses, e.g. (-i)**4.

https://docs.python.org/2/reference/expressions.html#operator-precedence https://docs.python.org/3/reference/expressions.html#operator-precedence

Amber
  • 507,862
  • 82
  • 626
  • 550
  • 3
    this is imho a bad implementation. if I write: -2 ** 2, I am stating the negative number to the power of 2, not the minus operator.... – Otto Nascarella Apr 06 '17 at 21:46
4

You can use the pow() function from math.

import math
i = 3
math.pow(-i,4)

This will yield a positive value.

As stated here: Exponentials in python x.**y vs math.pow(x, y), this option (or just build in pow()) would be ideal if you want to always produce a float.

Community
  • 1
  • 1
Malonge
  • 1,980
  • 5
  • 23
  • 33
  • This is somewhat overkill for something that could be handled via parentheses - `(-i)**4`. – Amber Dec 31 '14 at 21:55
  • this might not necessarily be an overkill, it depends on the interpreter implementation, but the question is not about how to compute an exponent. – akonsu Dec 31 '14 at 21:58
3

The power operator (**) has a higher precedence than the unary negation (-) operator. -i**4 is evaluated as -(i**4) - i.e., you take 3 up to the power of four, which is 81, and then negate it, resulting in -81.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

You have to do (-i)**4 to get a positive result.

The *'s have higher priority than the '-'.

When in doubt, use parentheses. (or, as Amber suggested, refer to the language documentation)

Jobs
  • 3,317
  • 6
  • 26
  • 52