Precidence of unary plus/minus relative to exponentiation

Erik Max Francis max at alcyone.com
Tue Apr 1 17:28:27 EST 2003


"Warnes, Gregory R" wrote:

> The current operator precedence places unary plus and minus higher
> than
> exponentiation.  This leads the counterintuitive result:
> 
>         >>> -2.0**2.0
>         -4.0
> 
> Python's operator predicence is causing this to be interpreted as:
> 
>         >>> -(2.0**2.0)
> 
> rather than the normal mathematical precidence, which would give
> 
>         >>> (-2.0)**2.0
> 
> Is there a good reason for the Python order of precedence?

In programming languages, there's often variance on whether
exponentiation has higher precedence than unary negation.  The curious
thing about your question is that Python's choice _is_ the way that it's
done in mathematics; when you write

	  b
	-a

this means -(a**b), not (-a)**b [using Python's exponential operator
**].  This can easily be seen if you deal with polynomial, for instance;
in normal type, the hyphen for the unary negation operator is large, and
the superscribed exponent is small, so the negation stands out much more
strongly.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ A good indignation brings out all one's powers.
\__/ Ralph Waldo Emerson
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.




More information about the Python-list mailing list