(7+9)**(1/2) returns 1?


#1

Homework 1 tells us to print the answer to this. We are not told how to use math functions or modules, so I deprived myself of that liberty. In python 2, this returns 1. It should return 4 (it does in Python 3).

Note: (7+9)**(.5) works (but we are told not to simplify)


#2

Try (7+9)**(1/2.) If that doesn’t help clarify what’s happening let me know.


#3

Also try …

>>> 1 / 2

Anything exponentiated to the value that results from that operation yields 1.

Try …

>>> 1.0 / 2.0

… and you get a different result. The behavior of the / operator varies according to the types of the operands that you give it.


#4

Thanks Glenn. I think this is a clearer way to indicate what’s happening.


#5

Yes, using a float does work. I just wanted to note this for those who may not have realized the issue. Python 3 does yield 4 though. This is a major change between the versions.


#6

Yes, in Python 3, the / operator always performs float division, even if both operands are ints. To guarantee int division, you would use the // operator.