0

Can you explain what's happening with python shell..

>>> 6/7   
0

>>> -6/7   
-1

>>> -(6/7)    
0
timgeb
  • 76,762
  • 20
  • 123
  • 145
Star Rider
  • 107
  • 1
  • 6
  • 3
    Possible duplicate of [Negative integer division surprising result](http://stackoverflow.com/questions/5535206/negative-integer-division-surprising-result) – timgeb Jan 09 '16 at 12:56
  • http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python – Łukasz Rogalski Jan 11 '16 at 17:21

2 Answers2

1

With the / operator python always rounds to minus infinity (so to the "more negative" value) if you input integers, like stated in the python docs. This explains the described behavior.

So 6/7 would be 0.857... and gets rounded to 0 while -6/7 gives -0.857... and will be rounded to -1. Finally -0 equals 0.

AdmPicard
  • 439
  • 3
  • 14
1

If you want to perform floating point division you should set the following import at the top of your script or as the first line in your Python shell:

from __future__ import division

This will ensure that you get proper results. If you want to perform integer division use // instead.

polarise
  • 2,303
  • 1
  • 19
  • 28