1

I know a/b is floating point division and a//b is floor division in Python.
It's seen that int(a/b) also results the same as floor division if both numerators and denominators are positive number. But on trying -a//b and int(-a/b) yield different results. What are the internal operations?

>>> int(-5/3)
-1
>>> -5//3
-2

How different is int(a/b) from the equivalent floor division i.e, a//b?

Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
  • `floats` are rounded towards zero by default, so `int(-5/3)` ~ `int(-1.66)` ~ `-1`. – hilberts_drinking_problem Aug 12 '19 at 05:32
  • 2
    @hiroprotagonist I _think_ this is not an exact duplicate question, but the answer to the duplicate answers this one too. – Selcuk Aug 12 '19 at 05:32
  • @Selcuk the first answer addresses everything asked here i'd say? if you disagree i will reopen the question. – hiro protagonist Aug 12 '19 at 05:41
  • So `int(a/b)` rounds towards zero whereas `a//b` lowers the value obtained? Anyways the issue mentioned here wasn't asked there exactly. – Abhinav Kinagi Aug 12 '19 at 05:51
  • @hiroprotagonist As I mentioned in my comment, I agree that the _answer_ is sufficient even though the questions are not exactly the same. No objections to leaving it as it is. – Selcuk Aug 12 '19 at 06:01
  • @AbhinavKinagi You are right, `int` truncates the decimal portion, so you can say that it rounds towards zero. – Selcuk Aug 12 '19 at 06:01

1 Answers1

1

From int docs:

For floating point numbers, this truncates towards zero.

From // docs:

Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result.

awesoon
  • 32,469
  • 11
  • 74
  • 99