473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1738
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
<29**********************************@r60g2000hsc. googlegroups.com>,
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
14
by: Nils Grimsmo | last post by:
Why did round() change in Python 2.4? $ python2.3 Python 2.3.5 (#2, Jun 19 2005, 13:28:00) on linux2 >>> round(0.0225, 3) 0.023 >>> "%.3f" % round(0.0225, 3) '0.023' >>>
5
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the...
23
by: Simon Hengel | last post by:
Hello, we are hosting a python coding contest an we even managed to provide a price for the winner... http://pycontest.net/ The contest is coincidentally held during the 22c3 and we will be...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
3
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/thinking-outside-the-box-with-python/ . Introduction: I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.