python - Negative integer division surprising result -


in application encountered following , surprised results:

8/-7=-2 (both integers).

what means?

for actual values, i.e. 8.0/(-7.0), result -1.143.

your result using integer division being rounded down toward more negative value of -2. (this known "floor division")

this why perplexing answers of:

>>> 8/(-7) -2 >>> 8/7 1 

note: "fixed" in python 3, result of 8/(-7) -1.143. if have no reason using python 2, should upgrade. ;)

in python 3, if still want integer division, can use // operator. give same answer 8/(-7) in python 2.

here's python enhancement proposal on subject: pep 238 -- changing division operator


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -