Integer Division in Python 2 and Python 3

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.

different run results between python3 and python2 for the same code

The problem lies on this line:

temp1 = temp_phi / e

In Python 2, the / operator performs integer division when both its arguments are integers. That is, it is equivalent (conceptually) to floor(float(a) / float(b)), where a and b are its integer arguments. In Python 3, / is float-division regardless of the types of its arguments, and the behaviour of / from Python 2 is recreated by the // operator.

python3 division operator exactly like in python2

You can't make it work like that. You'll have to use / and // when appropriate.

If, for some reason, you need the "polymorphism" of the old operator, you can...

def div(a, b):
if isinstance(a, int):
return a // b
else:
return a / b

Division in Python 3 gives different result than in Python 2

/ is a different operator in Python 3; in Python 2 / alters behaviour when applied to 2 integer operands and returns the result of a floor-division instead:

>>> 3/2   # two integer operands
1
>>> 3/2.0 # one operand is not an integer, float division is used
1.5

Add:

from __future__ import division

to the top of your code to make / use float division in Python 2, or use // to force Python 3 to use integer division:

>>> from __future__ import division
>>> 3/2 # even when using integers, true division is used
1.5
>>> 3//2.0 # explicit floor division
1.0

Using either of these techniques works in Python 2.2 or newer. See PEP 238 for the nitty-gritty details of why this was changed.

Python2/3 give different answers to the same math operation...why? (calculating percent difference)

Python2 and Python3 treat the div operation for integers differently.

In python3 the div operation will treat integers and floats the same:

2/3 = 1.5
2.0/3 = 1.5
2/3.0 = 1.5
2.0/3.0 = 1.5

In python2 it will truncate the number after the floating points if both operands are integers:

2/3 = 1
2.0/3 = 1.5
2/3.0 = 1.5
2.0/3.0 = 1.5

Python 3 integer division

Try this:

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

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.



Related Topics



Leave a reply



Submit