Python Exponent Question

D

databyss

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
 
C

Carsten Haese

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

Robert Kern

databyss said:
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
 
T

Tim Chase

databyss said:
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

Order of operations and proximity:
256

Apparently Python assumes the former and you assume the latter.
When in doubt about order of operations, use parens. And even
when you *know* the order of operations, be kind to those who
will have to read your code later and put in the parens anyways.

-tkc
 
M

Michael J. Fromberger

databyss said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top