pow() vs ** [exponentiation operator]

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

pow() vs ** [exponentiation operator]

Postby howardleeharkness » Wed Oct 30, 2013 4:28 pm

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]

Postby metulburr » Wed Oct 30, 2013 4:34 pm

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
Type "help", "copyright", "credits" or "license" for more information.
>>> -5 ** 2
-25
>>> (-5) ** 2
25
>>>
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

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

Postby howardleeharkness » Wed Oct 30, 2013 5:12 pm

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]

Postby ochichinyezaboombwa » Wed Oct 30, 2013 8:02 pm

There is a particular precedence of operators in Python.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

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

Postby howardleeharkness » Wed Oct 30, 2013 8:20 pm

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".

Thanks for the link (I'm already working through that document & just hadn't gotten there yet).

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]

Postby ochichinyezaboombwa » Wed Oct 30, 2013 8:49 pm

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: 200
Joined: Tue Jun 04, 2013 7:53 pm

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

Postby micseydel » Wed Oct 30, 2013 9:17 pm

Python is both strongly and dynamically typed. C++ is strongly and statically typed. Lisp is weakly and dynamically typed.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1443
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

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

Postby XndrK » Fri Nov 01, 2013 3:59 pm

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

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

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

Postby micseydel » Fri Nov 01, 2013 4:09 pm

Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1443
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

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

Postby zeycus » Mon Nov 04, 2013 4:33 pm

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.
Image

Live long and prosper.
Spock
User avatar
zeycus
 
Posts: 23
Joined: Sun Feb 17, 2013 10:30 am
Location: Madrid

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

Postby zeycus » Mon Nov 04, 2013 4:39 pm

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 - :shock:
Image

Live long and prosper.
Spock
User avatar
zeycus
 
Posts: 23
Joined: Sun Feb 17, 2013 10:30 am
Location: Madrid


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests