0

In Python 2.x ,the integer division-7/3 should output -2,right?.Reason:since both the numerators and denominators are integers,the division is carried out and digits after decimal are truncated.so in this case, -7/3=-2.3333333....so,python should output -2.but it gives output -3.can anyone please explain why?

explorer
  • 27
  • 1
  • 8
  • 1
    From the [docs](https://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations): "the result is that of mathematical division with the ‘floor’ function applied to the result" – Mark Dickinson May 29 '17 at 06:13
  • 1
    BTW, I recommend that you always use the `//` operator for integer division, since its meaning is unambiguous across Python versions, and doesn't care whether `from __future__ import division` is in place. – Mark Dickinson May 29 '17 at 06:14

4 Answers4

4

"and digits after the decimal are truncated"

No. That's not quite true. Python 2.x implements floor division for integers (and // is floor division in python2.x and python3.x). For positive numbers, this is the same as truncating the numbers after the decimal point. For negative numbers however, you get a different result (as you've noticed).

Note that this is different behavior than you might get with C (for example). The rational behind this decision is described by Guido in this blog post

The integer division operation (//) and its sibling, the modulo operation (%), go together and satisfy a nice mathematical relationship (all variables are integers):

a/b = q with remainder r

such that

b*q + r = a and 0 <= r < b

(assuming a and b are >= 0).

If you want the relationship to extend for negative a (keeping b positive), you have two choices: if you truncate q towards zero, r will become negative, so that the invariant changes to 0 <= abs(r) < otherwise, you can floor q towards negative infinity, and the invariant remains 0 <= r < b.

In mathematical number theory, mathematicians always prefer the latter choice ...

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • good explanation – Gahan May 29 '17 at 06:12
  • So,basically Python integer division is floor function of normal(the one we do in mathematics) integer division,right?That is why the answer(floor function(-2.333...)=-3) is -3.... – explorer May 29 '17 at 06:14
  • 1
    @explorer -- Yes. In python terminology, there is "true division" and "floor division" where "floor division" is the floor function applied to the result of true division. In python2.x, integers implemented "floor division". In python3.x that changed and the _best_ way to recover "floor division" behavior is to use the floor division operator (`//`). – mgilson May 29 '17 at 06:16
  • I neglected to mention that that you can (and **should**) use the floor division operator in python2.x code too. :-) – mgilson May 29 '17 at 06:22
1

It is important to realize that -7/3 is parsed as (-7)/3 rather than as -(7/3), whereupon the floor division rule explained in other answers results in the quotient evaluating to -3.

Leon
  • 31,443
  • 4
  • 72
  • 97
0
>>> 7/2
3
>>> -7/2
-4
>>> 7/3
2
>>> -7/3
-3
>>> 

you are getting floor values after division hence for negative number -3 is floor value and -2 is ceil value because -3<-2

Gahan
  • 4,075
  • 4
  • 24
  • 44
0

I guess Python 2.7 automatically gives the floor values. You could import numpy and then do a np.ceil(-7/3)

deepayan das
  • 1,567
  • 3
  • 14
  • 21
  • In Python 2, `np.ceil(-7/3)` will be `-3.0`, which I doubt is what you intend. (BTW, why import numpy here when the `math` module already has a `ceil` function?) – Mark Dickinson May 29 '17 at 06:16
  • yes, I cross checked `np.ceil(-7/3)` indeed gives -3, but so does ceil function from math module. I just use numpy because that's I am used to. Btw can you please explain as to why `ceil(-7/3)` and `floor(-7/3)`, both give -3 as the answer. – deepayan das May 29 '17 at 06:21
  • Well, as the OP already observed, `-7/3` is `-3`, so `ceil(-7/3)` is `ceil(-3)` (the argument of the function is evaluated before it's passed to the function). – Mark Dickinson May 29 '17 at 06:34
  • Hey thanks for the explanantion – deepayan das May 29 '17 at 08:44