Python 3 Integer Division

Python 3 integer division

Try this:

a = 1
b = 2
int_div = a // b

Python 3 int division operator is returning a float?

From PEP-238, which introduced the new division (emphasis mine):

Semantics of Floor Division


Floor division will be implemented in all the Python numeric types,
and will have the semantics of:

a // b == floor(a/b)

except that the result type will be the common type into which a and
b are coerced before the operation.

Specifically, if a and b are of the same type, a//b will be of that type too. If the inputs are of different types, they are first
coerced to a common type using the same rules used for all other
arithmetic operators.

In particular, if a and b are both ints or longs, the result has the
same type and value as for classic division on these types (including
the case of mixed input types; int//long and long//int will both
return a long).

For floating point inputs, the result is a float. For example:

3.5//2.0 == 1.0

For complex numbers, // raises an exception, since floor() of a
complex number is not allowed.

For user-defined classes and extension types, all semantics are up to
the implementation of the class or type.

So yes, it is supposed to behave that way. "// means integer division and should return an integer" - not quite, it means floor division and should return something equal to an integer (you'd always expect (a // b).is_integer() to be true where either operand is a float).

Python 3 integer division. How to make math operators consistent with C

You could use the // operator. It performs an integer division, but it's not quite what you'd expect from C:

A quote from here:

The // operator performs a quirky kind of integer division. When the
result is positive, you can think of
it as truncating (not rounding) to 0
decimal places, but be careful with
that.

When integer-dividing negative numbers, the // operator rounds “up”
to the nearest integer. Mathematically
speaking, it’s rounding “down” since
−6 is less than −5, but it could trip
you up if you were expecting it to
truncate to −5.

For example, -11 // 2 in Python returns -6, where -11 / 2 in C returns -5.
I'd suggest writing and thoroughly unit-testing a custom integer division function that "emulates" C behaviour.

The page I linked above also has a link to PEP 238 which has some interesting background information about division and the changes from Python 2 to 3. There are some suggestions about what to use for integer division, like divmod(x, y)[0] and int(x/y) for positive numbers, perhaps you'll find more useful things there.

Why does integer division yield a float instead of another integer?

Take a look at PEP-238: Changing the Division Operator

The // operator will be available to request floor division unambiguously.

Integer division in Python 2 and Python 3

In Python 2.7, the / operator is integer division if inputs are integers.

If you want float division (which is something I always prefer), just use this special import:

from __future__ import division

See it here:

>>> 7 / 2
3
>>> from __future__ import division
>>> 7 / 2
3.5
>>>

Integer division is achieved by using //, and modulo by using %:

>>> 7 % 2
1
>>> 7 // 2
3
>>>

As commented by user2357112, this import has to be done before any other normal import.

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.

Handling long integer-divisions in python

In Python 3 integer division is no longer the default. If you would like to use integer division in Python 3 you need to use the // operator rather than /.

>>> 232305724979817822068561403710502859128427941904411569030164388864727209 / 123456789789456123
1.8816763774272217e+54

>>> 232305724979817822068561403710502859128427941904411569030164388864727209 // 123456789789456123
1881676377427221798261593926550420634004875213170503083

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.

Integer division in Python 3 - strange result with negative number

// is not integer division, but floor division:

7/-3  -> -2.33333...
7//-3 -> floor(7/-3) -> floor(-2.33333...) -> -3

PEP 238 on Changing the Division Operator:

The // operator will be available to request floor division unambiguously.

See also Why Python's Integer Division Floors (thanks to @eugene y) - Basically 7//-3 is -7//3, so you want to be able to write:

-7 = 3 * q + r

With q an integer (negative, positive or nul) and r an integer such that 0 <= r < 3. This only works if q = -3:

-7 = 3 * (-3) + 2


Related Topics



Leave a reply



Submit