Floor Division with Negative Numbers in Python Programming

The floor division is the arithmetic operation used to divide two numbers and return the quotient rounded down to the nearest integer. It is denoted by the double forward slash"//" in Python. When both operands of floor division are positive, the result is the same as regular integer floor division. However, when one or both of the operands are negative, the output is different from the normal one. Let's see that:

When performing floor division with negative numbers, the result is rounded toward negative infinity. In other words, the quotient is rounded to the nearest integer if it is less than or equal to the precise result( Without decimal). Let's take an example to understand this better:


>>> -7 // 3
-3

In the above code, -7 // 3 returns -3. The exact result of -7/3 is -2.333..., but the result of floor division is rounded towards negative infinity to the nearest integer that is less than or equal to the exact result, which is -3.

Similarly, if we perform floor division with a positive number and a negative number, the result is rounded toward negative infinity:


>>> 7 // -3
-3

In the above code, 7 // -3 returns -3. The exact result of 7/-3 is -2.333..., but the result of floor division is rounded towards negative infinity to the nearest integer that is less than or equal to the exact result, which is -3.

When both operands of floor division are negative(-), the result is rounded toward positive infinity. This indicates that the quotient is rounded up to the next larger or equal to the exact result integer. let's see the code:


>>> -7 // -3
2

In the above code,  -7 // -3 returns 2. The exact result of -7/-3 is 2.333..., but the result of floor division is rounded towards positive infinity to the nearest integer that is greater than or equal to the exact result, which is 2.

When a negative number is divided using floor division, the result is rounded toward negative infinity. This means that the quotient is shifted towards negative infinity, which results in a smaller absolute value. For example, -7 // 2  output is  -4 instead of -3, because -3 is closer to zero than -4.

Please keep one thing in mind the behavior of floor division with negative numbers can be different for different programming languages or even different versions of Python. As a result, there are changes in different outputs.

Let's take one more example to understand it better.


What is the output of the following code?

>>> 1+(2-3)*4**5//6


When evaluating the above code 1+(2-3)*4**5//6, Python follows the order of operations:

First, it evaluates brackets that are (2-3)

Second, it evaluates the exponentiation operator **. 4^^5.

Next, it evaluates the multiplication operator *. Since there are no parentheses indicating a different order of operations, it multiplies 1024 by the result of (2-3), which is -1.

Then, it evaluates the floor division operator //, which divides the product of -1024 by 6 and rounds down to the nearest integer. This results in -171.

Finally, it evaluates the addition operator +, which adds 1 to -171, resulting in a final answer of -170.

Therefore, the final result of the expression 1+(2-3)*4**5//6 is -170.

Next Post Previous Post