Home > Blockchain >  difference between int(a//x) and int(a/x) in python3
difference between int(a//x) and int(a/x) in python3

Time:12-26

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) .

CodePudding user response:

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.

CodePudding user response:

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.

  • Related