Difference Between '/' and '//' When Used For Division

What is the difference between '/' and '//' when used for division?

In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.

In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.

Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.

You can find a detailed description at PEP 238: Changing the Division Operator.

What is the reason for difference between integer division and float to int conversion in python?

Consistency.

You'll need to follow some very basic and seemingly irrelevant explanations to understand it.

In school you have learned division with a remainder. And you have done calculations like this:

8 ÷ 4 = 2 R 0
7 ÷ 4 = 1 R 3
6 ÷ 4 = 1 R 2
5 ÷ 4 = 1 R 1
4 ÷ 4 = 1 R 0
3 ÷ 4 = 0 R 3
2 ÷ 4 = 0 R 2
1 ÷ 4 = 0 R 1
0 ÷ 4 = 0 R 0
^------ This is the result of x // 4
^-- This is the result of x % 4 (modulo)

Later, you have learned divisions for real numbers:

8 ÷ 4 = 2.0
7 ÷ 4 = 1.75
6 ÷ 4 = 1.5
5 ÷ 4 = 1.25
4 ÷ 4 = 1.0
3 ÷ 4 = 0.75
2 ÷ 4 = 0.5
1 ÷ 4 = 0.25
0 ÷ 4 = 0.0
^--- Note that the number in front of the . is int(x/4)

Until this point, you might believe that x // 4 and int(x/4) always give the same result. That's your current understanding of the situation.

However, have a look what happens in the integer division: the number behind R cycles from 3, 2, 1 to 0 and then restarts: 3, 2, 1, 0. The number in front of the R decreses every 4th step.

So, how will it go on?

 8 ÷ 4 =  2 R 0
7 ÷ 4 = 1 R 3
6 ÷ 4 = 1 R 2
5 ÷ 4 = 1 R 1
4 ÷ 4 = 1 R 0
3 ÷ 4 = 0 R 3
2 ÷ 4 = 0 R 2
1 ÷ 4 = 0 R 1
0 ÷ 4 = 0 R 0
-1 ÷ 4 = -1 R 3
^------ We have to decrease now, because we already have 0 four times
^-- We have to restart the cycle at 3

At the same time, the real number division gives us:

-1 ÷ 4 = -0.25
^----- There is still a 0 in front of the .

That's why -1 // 4 gives -1 but int(-1/4) gives 0.

Is there any motivation for the differences between the functions?

Well, they serve different purposes: // is part of an integer calculation with remainders and int() gives you the part in front of the . of a real number operation.

You decide what you want to calculate, then you decide which operator to use in Python to get the correct result.

Good question. Keep on learning.

Difference between // and / in Python 2

// is the floored-division operator in Python. The difference is visible when dividing floating point values.

In Python2, dividing two ints uses integer division, which ends up getting you the same thing as floored division. However, you can still use // to get a floored result of floating point division. live example

# Python 2
>>> 10.0 / 3
3.3333333333333335
>>> 10.0 // 3
3.0

However, in Python3, dividing two ints results in a float, but using // acts as integer division. live example

# Python3
>>> 10 / 3
3.3333333333333335
>>> 10 // 3
3

If you are (still) working in Python2, but want to someday convert to Python3, you should always use // when dividing two ints, or use from __future__ import division to get the Python3 behavior in Python2

Floored division means round towards negative infinity. This is the same as truncation for positive values, but not for negative. 3.3 rounds down to 3, but -3.3 rounds down to -4.

# Python3
>>> -10//3
-4
>>> 10//3
3

What is Difference between Sample division and floor division? python

// is integer division.

>>> 6//4
1
>>> 6.0//4
1.0

The behavior of / varies between Python 2 and Python 3.
In general, / is regular floating point division, except for the case where two integers are divided in Python 2, in which case / falls back to the behavior of //.

Python 2:

>>> 6/4
1
>>> 6.0/4
1.5

Python 3:

>>> 6/4
1.5
>>> 6.0/4
1.5

What is the difference between '' and ' / ' , shifting and division in java

No, absolutely not the same.

You can use >> to divide, yes, but just by 2, because >> shift all the bits to the right with the consequence of dividing by 2 the number.

This is just because of how binary base operations work. And works for unsigned numbers, for signed ones it depends on which codification are you using and what kind of shift it is.

eg.

122 = 01111010 >> 1 = 00111101 = 61

What is the difference between '/' operator and %/% operator in r programming language?

/ does the usual division.

Regarding %/%, the documentation states:

%% indicates x mod y and %/% indicates integer division. It
is guaranteed that x == (x %% y) + y * ( x %/% y ) (up to
rounding error) unless y == 0 […]

To find the documentation for such operators, enter the following on the R console:

help("%/%")


Related Topics



Leave a reply



Submit