64

In my application I encountered the following and was surprised by the results:

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

What does this mean?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vivek S
  • 5,384
  • 8
  • 51
  • 72

5 Answers5

61

For the actual values, i.e. 8.0/(-7.0), the result is roughly -1.143.

Your result using integer division is being rounded down toward the more negative value of -2. (This is also known as "Floor division")

This is why you will get the somewhat perplexing answers of:

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

Note: This is "fixed" in Python 3, where the result of 8/(-7) would be -1.143. So if you have no reason to be using Python 2, you should upgrade. ;)

In Python 3, if you still want integer division, you can use the // operator. This will give you the same answer as 8/(-7) would in Python 2.

Here's a Python Enhancement Proposal on the subject: PEP 238 -- Changing the Division Operator

Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
  • 2
    @Ira: Yup! They changed this for python 3 though. – Chris Cooper Apr 04 '11 at 06:32
  • It seems like it should round towards zero if it's going to round. – intuited Apr 04 '11 at 07:51
  • 1
    @intuited: Well, seems like, perhaps, but that's only if you don't think like a computer. 5/2 is the same thing as a one bit right shift, you get 2 in both cases. -5/2 should then be the same as a right shift of -5, and that's -3. The problem here is of course that Python is a high level language, and you shouldn't have to know this. Which is why integer division was wrongin the first place, which has now been fixed. – Lennart Regebro Apr 04 '11 at 08:28
  • 4
    @Lennart: so you divide -5 by 2 and get -3 with a remainder of 1. "To settle our 5 emu debt to you, we will each pay you 3 emus, and you will provide us with one emu to wing us both home godspeed". Sure, makes sense I guess. – intuited Apr 04 '11 at 08:55
  • @intuited: Ah well, yes, the *remainder* I agree is quite a surprise. :-) – Lennart Regebro Apr 04 '11 at 09:01
  • 1
    @intuited: a should equal (a//b)*b + a%b. Thus -5//2 == -3. – Neil G Apr 07 '11 at 04:35
  • @Neil: I would have expected that `-5 // 2 == -2` and `-5 % 2 == -1`. In other words, we still owe you an emu for next time. – intuited Apr 07 '11 at 04:57
  • 1
    @intuited: that makes sense, but don't you want `-5 % 2 == 5 % 2` since they're in the same equivalence class? – Neil G Apr 07 '11 at 07:24
  • 3
    @LennartRegebro: For all x and y within the realm of natural numbers or real numbers, using the division operator defined for the realm, (n+d)/d = (n/d)+1. Python defines integer division in such a way that that useful axiom holds even when n is negative. Even if some people might expect that integers should borrow the (-n)/d = -(n/d) from real numbers, that axiom is very seldom useful. When trying to extract digits from a number, having "-123 mod 10" yield 7 isn't terribly useful, but having it yield -3 is no better. – supercat Dec 19 '13 at 09:04
  • @intuited I think that's just how it works in maths (modulo arithmetic). Programming languages are mostly just respecting this definition. – xji May 22 '16 at 16:40
  • Quick question, how would I make say -3//4 return 0 and not -1 in Python 3? I do not want it to round down when negative. Essentially I want to do this - "division should truncate toward zero." – Parth Feb 07 '23 at 15:29
23

Python always does the "floor division" for both negative numbers division and positive numbers division.

That is

1/10 = 0
1/-10 = -1

But sometime we need 1/-10 to be 0

I figure out it can be done by using the float division first then cast result to int, e.g.

int(float(1)/-10) = 0

That works fine for me, no need to import the future division or upgrade to Python 3

Hope it can help you~

ZH.Jay
  • 231
  • 2
  • 2
8

To have Python automatically convert integer division to float, you can use:

from __future__ import division

Now:

8/-7=-1.1428571428571428

This feature is not in the standard Python 2 not to break existing code that relied on integer division.

However, this is the default behavior for Python 3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrea Zonca
  • 8,378
  • 9
  • 42
  • 70
3

When both values are integers when dividing Python uses Floor division.

Lossy
  • 142
  • 1
  • 5
  • But only for Python 2? Perhaps [update your answer](https://stackoverflow.com/posts/5535242/edit) (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today)? – Peter Mortensen Jan 29 '22 at 21:50
-1

In Python, / operator is for integer division. You can look at it as float division followed by a floor operation.

For example,

8/7 == floor(8.0/7.0) == 1

8/-7 == floor(8.0/-7.0) == -2

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rumple Stiltskin
  • 9,597
  • 1
  • 20
  • 25
  • 1
    No, / is not for integer division, it's for division. However, in Python 2 dividing two integers would return an integer. This is surprising and has been fixed in Python 3. – Lennart Regebro Apr 04 '11 at 08:29
  • 3
    @Lennart: Not exactly a shock for C programmers, nor for anyone who'd read the manual, and not "wrong". – John Machin Apr 04 '11 at 08:52
  • 1
    @John: It violates core principles of Python and hence can be called "wrong" in the context of Python. – Lennart Regebro Apr 04 '11 at 09:02
  • @stackoverflowuser2010 No, it's *unexpected* for a C/C++ programmer that does not know Python. It is however not "wrong" for them either. – Lennart Regebro Aug 31 '16 at 08:16
  • But only for Python 2? Perhaps [update your answer](https://stackoverflow.com/posts/5535261/edit) (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today)? – Peter Mortensen Jan 29 '22 at 21:49