472,993 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 1720
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.