6

I know that integer division will always return the same answer as truncation of a floating point result if the numbers are both positive. Is it true if one or both of them are negative?

I was just curious to know if there was an integer division expression that would return the same results in Python 2 and Python 3 (and yes, I know about from __future__ import division).

P.S. Let's ignore floating point overflow for the moment.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    *" an integer division expression that would return the same results in Python 2 and Python 3"* Come again? – NullUserException Oct 11 '12 at 16:02
  • 1
    @NullUserException, in Python 3 if you use `/` to divide two integers you will get a floating point result. This is a huge change from Python 2. `//` is now necessary to get the old behavior. – Mark Ransom Oct 11 '12 at 16:06
  • I still dont understand why they auto upcast to a float .... seems silly to me... I mean `int/int = int` is a core tenet of CS ... – Joran Beasley Oct 11 '12 at 16:10
  • I still don't get it. *"if there was an integer division expression that would return the same results in Python 2 and Python 3"* For any ints a,b such that a%b == 0, the results are the same. – NullUserException Oct 11 '12 at 16:11
  • 2
    @JoranBeasley Rationale here: http://docs.python.org/release/2.2.3/whatsnew/node7.html – NullUserException Oct 11 '12 at 16:11
  • @JoranBeasley: I don't know what their rationale is, but it seems clear to me that integer division and floating point division are different operators and should be treated as such. Having to cast one of the operands to double in C and C++ has always felt awkward and wrong to me. – Omnifarious Oct 11 '12 at 16:13
  • thanks ... still think its strange ... – Joran Beasley Oct 11 '12 at 16:13
  • @NullUserException, thanks for the link. I didn't realize the `//` operator was added all the way back in 2.2. – Mark Ransom Oct 11 '12 at 16:15
  • 4
    @JoranBeasley: you really think that in a duck-typed language you shouldn't just be able to treat a number as a number and assume `/` does actual division just because C is statically typed? Integer division is hardly a "core tenet of CS". – Wooble Oct 11 '12 at 16:58
  • @MarkRansom: What needs to be added to my answer to make it acceptable? – Omnifarious Oct 12 '12 at 04:16
  • @Omnifarious absolutely nothing. I just make it a habit not to accept something right away. You got my +1 long ago. P.S. great answer. – Mark Ransom Oct 12 '12 at 04:43
  • @MarkRansom: Thanks! I thought it was something like that. I have the same habit. I just wanted to make sure I answered your question completely. – Omnifarious Oct 12 '12 at 06:20

1 Answers1

10

It is not true in Python 3, and you can test it for yourself:

>>> int(-1/3) == -1//3
False

Integer division and modulo of a and b giving q (quotient) and r (remainder) respectively will always return numbers that satisfy b*q + r == a and (a*b)>0 == q>0 (i.e. a*b and q have the same sign) and abs(r) < abs(q). The expression int(q) simply always rounds towards 0 if q is a floating point number.

It will always be true for Python 2 unless you do from __future__ import division, but that's because a/b == a//b if a and b are integers in Python 2.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194