Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


(7+9)**(1/2): Why isn't this returning 4?

  • You cannot reply to this topic
12 replies to this topic

#1 Tutorial Doctor   Members   -  Reputation: 1983

Like
0Likes
Like

Posted Today, 07:02 PM

I want to be fair with the homework I am doing, which is asking me to write an equation using Python syntax (they did not tell us how to import modules and how to access the square root functions, so I am depriving myself of that liberty.)

 

According to the precedence rules, parenthesis are first, and then exponents. 

 


They call me the Tutorial Doctor.


Sponsor:

#2 Nypyren   Crossbones+   -  Reputation: 6662

Like
2Likes
Like

Posted Today, 07:12 PM

Does python evaluate (1/2) as zero like most other languages?

#3 ChaosEngine   Crossbones+   -  Reputation: 2965

Like
0Likes
Like

Posted Today, 07:19 PM

er, it does return 4? 

Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> (7+9)**(1/2)
4.0

at least, it does in the (admittedly old) version I have installed.


if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#4 Bacterius   Crossbones+   -  Reputation: 10573

Like
1Likes
Like

Posted Today, 07:21 PM

He is probably using Python 2:
 

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (7+9)**(1/2)
1

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 Tutorial Doctor   Members   -  Reputation: 1983

Like
0Likes
Like

Posted Today, 07:25 PM

Yes. Python 2.7.8

Python 2.7.8 (v2.7.8:ee879c0ffa11...

 

Is there a reason for this?

 

(7+9)**(.5) works, but they said not to simplify. 


Edited by Tutorial Doctor, Today, 07:27 PM.

They call me the Tutorial Doctor.


#6 SiCrane   Moderators   -  Reputation: 10178

Like
1Likes
Like

Posted Today, 07:30 PM

So Python 2 follows most programming languages in that arithmetic operations on two integers give an integer result. So 1/2 is 0, and anything raised to a 0 power is 1. Python 3 came along can said that most people find that 1/2 being 0 is unnatural so will let integer arithmetic yield floating point results, so 1/2 is 0.5. 



#7 Álvaro   Crossbones+   -  Reputation: 15407

Like
0Likes
Like

Posted Today, 07:30 PM

If you mean "1 the double-precision floating-point number" and not "1 the integer", you should write 1.0.

>>> (7+9)**(1.0/2)
4.0


#8 Álvaro   Crossbones+   -  Reputation: 15407

Like
1Likes
Like

Posted Today, 07:32 PM

So Python 2 follows most programming languages in that arithmetic operations on two integers give an integer result. So 1/2 is 0, and anything raised to a 0 power is 1. Python 3 came along can said that most people find that 1/2 being 0 is unnatural so will let integer arithmetic yield floating point results, so 1/2 is 0.5.


Oh, I didn't know this (admittedly, I don't know much about Python, period). Python 3 also provides the operator // that will perform integer division. I like this compromise.

#9 Tutorial Doctor   Members   -  Reputation: 1983

Like
0Likes
Like

Posted Today, 07:49 PM

If you mean "1 the double-precision floating-point number" and not "1 the integer", you should write 1.0.
 

>>> (7+9)**(1.0/2)
4.0

 The exercise told us to use 1, although I knew 1.0 would yield the right answer. I think it is a fault with the homework, because they were not specific. If I was new to Python, I would be confused by their request. They also told us not to simplify. 


They call me the Tutorial Doctor.


#10 Nypyren   Crossbones+   -  Reputation: 6662

Like
1Likes
Like

Posted Today, 08:28 PM

So Python 2 follows most programming languages in that arithmetic operations on two integers give an integer result. So 1/2 is 0, and anything raised to a 0 power is 1. Python 3 came along can said that most people find that 1/2 being 0 is unnatural so will let integer arithmetic yield floating point results, so 1/2 is 0.5.


That's one HELL of a breaking change... does that only hold true for literals, or does it do that for variables as well?

Edited by Nypyren, Today, 08:28 PM.


#11 SiCrane   Moderators   -  Reputation: 10178

Like
0Likes
Like

Posted Today, 08:36 PM

Any integer division. I can't think of any place Python treats literals differently as other values of their type.



#12 ChaosEngine   Crossbones+   -  Reputation: 2965

Like
0Likes
Like

Posted Today, 08:40 PM

 

If you mean "1 the double-precision floating-point number" and not "1 the integer", you should write 1.0.
 

>>> (7+9)**(1.0/2)
4.0

 The exercise told us to use 1, although I knew 1.0 would yield the right answer. I think it is a fault with the homework, because they were not specific. If I was new to Python, I would be confused by their request. They also told us not to simplify. 

 

 

So why did you decide to use a legacy version of python? Python 3 has been out for over 6 years.

 

 

Short version: Python 2.x is legacy, Python 3.x is the present and future of the language

Edited by ChaosEngine, Today, 08:43 PM.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#13 Tutorial Doctor   Members   -  Reputation: 1983

Like
0Likes
Like

Posted Today, 09:44 PM


So why did you decide to use a legacy version of python? Python 3 has been out for over 6 years.

 

It is homework provided by an MOOC course by MIT, who say we can actually only use 2.6x because that is what they use. I read on a forum that we can use 2.7.8. But it seems both will not yield 4. 

 

Now that I know this though, I use the Pythonista app on the iPad which has not (and seems will not) be upgraded to version 3. 


They call me the Tutorial Doctor.






PARTNERS