python - What is the difference between 3/2 and -3/2? -


I am beginning with programming and Python. I am conducting some simple mathematics so 3/2 gives 1 in Python interpreter as we know but -3 / 2 Gives -2 . Can you point out the difference here?

In Python 2, / does it mean integer division That result, if this is not an integer, then the next integer value is rounded when the value is negative, it naturally rounds to the maximum number of negative numbers.

Intensely, the result of the integer division is the result of the float division. For this reason, the integer division is also commonly known as.

  floor (1.5) # returns 1.0 floor (-1.5) # returns -2.0  

__future__ import at the top of your module By entering from partition, it is possible to turn this behavior into Python 2. This will point to the import / operator only to the correct partition (float division) and enable the clear floor division (integer partitions) with the // operator. These relations are standard in Python 3. __future__ import division print (3/2) # 1.5 print (3/2) # 1


In the comments as a dune of notes, it is worth noting that the - has more priority than / , and therefore is equal to -3/2 (- 3) / 2 Instead of - (3/2) . If the partition was first implemented, the result would actually be -1 .


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -