0

When solving a problem in leetcode, I found:

>>> -6/132
-1

in python, which seems a bit weird, as it behaves different from c++ and java.

It seems that c++ and java is using round to zero mechanism and python is using round to -∞ mechanism.

Did python do this for any special purposes?

WKPlus
  • 6,955
  • 2
  • 35
  • 53
  • try -6//132 in Python2 or -6/132 in Python3 – Clodion Jul 27 '15 at 12:43
  • Actually, I want to ask why python do **round to -∞** instead of **round to zero** as c++ and java, but no such answer in [this question](http://stackoverflow.com/questions/5535206/python-negative-integer-division-surprising-result). – WKPlus Jul 27 '15 at 12:46
  • What do you mean *"why"*? It's just a choice. See e.g. http://stackoverflow.com/q/19517868/3001761 – jonrsharpe Jul 27 '15 at 12:47
  • "Integer division" is not a well-defined concept and each language's specification can choose flooring or truncation towards 0 as its defined result for the operation. --- This comment is clear enough. – WKPlus Jul 27 '15 at 12:59
  • Have to disagree. It is a well-defined concept, and it's not just a choice. The correct thing to do is heavily context-dependent. For example, if you are doing signal processing (which is often done with fixed point, aka integers), rounding towards zero may add unacceptable distortion to a signal, as the "bucket" at zero is now wider than all the other buckets. – Patrick Maupin Jul 27 '15 at 13:18

0 Answers0