## pow() vs ** [exponentiation operator]

This is the place for queries that don't fit in any of the other categories.

### pow() vs ** [exponentiation operator]

I was toying around with python 2.7.3 (that's the version installed on my client's system), and typed in

-5 ** 2

Which gave the answer -25, which is not correct (a even exponent should always result in a positive number). I checked it with 2.7.5 on my laptop, and got the same answer. pow(-5,2) gives the correct answer on both implementations.

Is this a feature, or a bug?
howardleeharkness

Posts: 3
Joined: Wed Oct 30, 2013 3:39 pm
Location: Plano, TX

### Re: pow() vs ** [exponentiation operator]

In a calculator you have to wrap the base number in parenthesis, in python it is the same way

Code: Select all
metulburr@ubuntu:~\$ python3
Python 3.3.1 (default, Sep 25 2013, 19:29:01)
[GCC 4.7.3] on linux
>>> -5 ** 2
-25
>>> (-5) ** 2
25
>>>
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: pow() vs ** [exponentiation operator]

Thank you for the prompt response. I was a bit surprised by the answer, but at least I now have a workaround for this "feature." I just have to remember to always enclose negative numbers in parens. Although pow(-5,2) works just as well as pow((-5),2), and -5 * -5 gives the same answer as (-5) * (-5).

I was surprised about the need for parens on a calculator. I haven't used a pocket calculator in over a decade, but I started ages ago on HP/RPN calculators, which did not require paren entry. I see that the Windows calculator wants to enter the parens for you. Plus, Windows calculator doesn't appear to have an exponentiation operator that you can enter via the keyboard, although you can use the appropriately-labeled button (which will square -5 correctly even when entered sans parens).

However, I did find a calculator which requires parens to get the right answer for (-5)^2 -- it's the one on my Android smartphone. Just never ran into that before.
howardleeharkness

Posts: 3
Joined: Wed Oct 30, 2013 3:39 pm
Location: Plano, TX

### Re: pow() vs ** [exponentiation operator]

There is a particular precedence of operators in Python.
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

### Re: pow() vs ** [exponentiation operator]

Ok, *now* I understand what was going on!

The precedence of -5**2 is -(5**2)

I had completely overlooked the precedence issue (C/C++ doesn't have an exponentiation operator, and I've been working in C/C++ so long I had forgotten about the precedence of ** in FORTRAN -- which is the same as in Python). Now that I have been reminded of that issue, it's much easier to remember than a completely arbitrary (and mysterious) rule like "put negative numbers in parens".

P.S. I'm hoping it doesn't take a long time to come up to fluency in Python, because I just got a bunch of it dumped in my lap, and I need to figure out what is going out PDQ. Been meaning to learn Python for a while, but just didn't have a really good excuse to do so (like getting paid to do it...). Now I do.
howardleeharkness

Posts: 3
Joined: Wed Oct 30, 2013 3:39 pm
Location: Plano, TX

### Re: pow() vs ** [exponentiation operator]

After C/C++ background, Python should be easy. Tabs, duck typing, elements of functional programming, list comprehensions and generators are in my opinion what makes it different from C++. You'll hate tabs at 1st; then you'll understand that they save your time and love it. Duck typing is a huge paradigm shift from strong-typed languages; it might confuse and even scare you at first; then you'll learn to love it.

Still, -- read the official tutorial. Also, there are tons of libraries that you normally don't need to remember by heart but need to know they exist and how to find documentation.

Python is awesome, welcome!
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

### Re: pow() vs ** [exponentiation operator]

Python is both strongly and dynamically typed. C++ is strongly and statically typed. Lisp is weakly and dynamically typed.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: pow() vs ** [exponentiation operator]

What does "strongly typed" mean?
Proverbs 26:14 describes me a bit too well.

Version: Python 3.4.3

https://github.com/4of92000
https://github.com/PythonForum/

XndrK

Posts: 208
Joined: Sat Jun 15, 2013 7:57 pm

### Re: pow() vs ** [exponentiation operator]

Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: pow() vs ** [exponentiation operator]

I had some laughs a while ago, when I noticed that in Excel, if in a cell you write "=-5^2", in fact it is evaluated to 25. So Excel has an unusual precedence of operators.

Live long and prosper.
Spock

zeycus

Posts: 23
Joined: Sun Feb 17, 2013 10:30 am

### Re: pow() vs ** [exponentiation operator]

And to make things worse, "=0-5^2" evaluates to -25. So the precedence of the power is between that of the unary -, and the binary -

Live long and prosper.
Spock

zeycus

Posts: 23
Joined: Sun Feb 17, 2013 10:30 am