1

Is there any difference between int(a//x) and int(a/x) in python3 if both a and x are integers. Recently I got a wrong answer in a contest when I used int(a/x) but my code was accepted when I used int(a//x) .

2 Answers2

1
x, y = 3, 4
print(int(x/y))
print(x//y)

returns

0 
0 

However:

x, y = -2, 4
print(int(x/y))
print(x//y)

returns

0 
-1 

So yes. In case one of your input variables is negative an integer, the output of your variable differs.

PeterD
  • 1,331
  • 12
  • 22
  • Thank you for your answer but in my case both x and y were integers. And I cast both (a/b) and (a//b) to int so they must have same type. – Pooya Ostovar Dec 25 '21 at 21:08
0

int(a/x) cuts off the decimals (truncates the number). It doesn't actually do division in the intfunction.

a//x divides to floor (rounds down). It uses the BINARY_FLOOR_DIVIDE in bytecode.

Ilmard
  • 86
  • 4