471,356 Members | 1,691 Online

# Python Exponent Question

I have a simple program and the output isn't what I expect. Could
somebody please explain why?

Here's the code:

#simple program
print "v = 2"
v = 2
print "v**v = 2**2 =", v**v
print "v**v**v = 2**2**2 =", v**v**v
print "v**v**v**v = 2**2**2**2 =", v**v**v**v
#end program

Here's the output:
>>>
v = 2
v**v = 2**2 = 4
v**v**v = 2**2**2 = 16
v**v**v**v = 2**2**2**2 = 65536
>>>
I would expect 2**2**2**2 to be 256
Dec 10 '07 #1
6 1663 On Mon, 2007-12-10 at 10:15 -0800, databyss wrote:
I have a simple program and the output isn't what I expect. Could
somebody please explain why?
[...]
v**v**v**v = 2**2**2**2 = 65536

I would expect 2**2**2**2 to be 256
Exponentiation is right-associative. 2**2**2**2 = 2**(2**(2**2)) =
2**(2**4) = 2**16 = 65536.

--
Carsten Haese
http://informixdb.sourceforge.net
Dec 10 '07 #2
:
v = 2
v**v = 2**2 = 4
v**v**v = 2**2**2 = 16
v**v**v**v = 2**2**2**2 = 65536

I would expect 2**2**2**2 to be 256
"... in an unparenthesized sequence of power and unary operators, the
operators are evaluated from right to left ..."
- http://docs.python.org/ref/power.html

So, 2**2**2**2 = 2**(2**(2**2)) = 65536

-[]z.
Dec 10 '07 #3
databyss wrote:
I would expect 2**2**2**2 to be 256
I stumbled upon it, too.

2**2**2**2 == 2**(2**(2**2)) == 2**16 == 65536

Christian
Dec 10 '07 #4
databyss wrote:
I have a simple program and the output isn't what I expect. Could
somebody please explain why?

Here's the code:

#simple program
print "v = 2"
v = 2
print "v**v = 2**2 =", v**v
print "v**v**v = 2**2**2 =", v**v**v
print "v**v**v**v = 2**2**2**2 =", v**v**v**v
#end program

Here's the output:

v = 2
v**v = 2**2 = 4
v**v**v = 2**2**2 = 16
v**v**v**v = 2**2**2**2 = 65536

I would expect 2**2**2**2 to be 256
Exponentiation is right-associative. I.e. 2**2**2**2 == 2**(2**(2**2))

The reason is that left-associativity is better written with multiplication.

(x**y)**z == x**(y*z)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Dec 10 '07 #5
databyss wrote:
I would expect 2**2**2**2 to be 256
I stumbled upon it, too.

2**2**2**2 == 2**(2**(2**2)) == 2**16 == 65536

Christian

Dec 10 '07 #6
In article
databyss <da******@gmail.comwrote:
I have a simple program and the output isn't what I expect. Could
somebody please explain why?

Here's the code:

#simple program
print "v = 2"
v = 2
print "v**v = 2**2 =", v**v
print "v**v**v = 2**2**2 =", v**v**v
print "v**v**v**v = 2**2**2**2 =", v**v**v**v
#end program

Here's the output:
>>
v = 2
v**v = 2**2 = 4
v**v**v = 2**2**2 = 16
v**v**v**v = 2**2**2**2 = 65536
>>

I would expect 2**2**2**2 to be 256
Python's ** operator associates to the right, not to the left; thus,

2 ** 2 ** 2 ** 2

.... really means

2 ** (2 ** (2 ** 2))

.... and not

((2 ** 2) ** 2) ** 2

.... as you seem to expect. As usual, you can enforce different
associations by explicitly including the parentheses.

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Dec 17 '07 #7

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 699 posts views Thread by mike420 | last post: by 89 posts views Thread by Radioactive Man | last post: by 14 posts views Thread by Nils Grimsmo | last post: by 5 posts views Thread by sankar | last post: by 23 posts views Thread by Simon Hengel | last post: by 7 posts views Thread by shellon | last post: by 1 post views Thread by Wayne Shu | last post: by 3 posts views Thread by Motoma | last post: by reply views Thread by sofiatarhonska | last post: by reply views Thread by Yacine Si Tayeb | last post: by reply views Thread by leo001 | last post: by reply views Thread by chloesad91 | last post: by reply views Thread by SwissProgrammer | last post: by reply views Thread by kjhyder | last post: by reply views Thread by YTAIGamer | last post: by reply views Thread by XIAOLAOHU | last post: by reply views Thread by PCGram | last post: by