Reputation: 1
why is negative 3 divided by two is negative two.And three divided by two is one in Python.I have tried it on IDLE and don't understand why.
why -3/2=-2 and 3/2=1 in Python
Upvotes: 0
Views: 2376
Reputation: 2058
Because the two numbers you are dividing are integers, python 2 floors the quotient of 3/2
. If you want to get a float as an answer, just do 3.0/2.0
instead. (note: you don't have to do this in python 3)
Upvotes: 2
Reputation: 114038
an int divided by an int is always floored to a new int .... (in python2x at least)
print math.floor(1.5),math.floor(-1.5)
(note that this is probably an oversimplification ....)
Upvotes: 0