Skip to content Skip to sidebar Skip to footer

Different Run Results Between Python3 And Python2 For The Same Code

when i run this python code in python3 it shows different results from python2? why there are different values? d = 0 x1 = 0 x2 = 1 y1 = 1 e=125 phi=238

Solution 1:

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.


Solution 2:

The reason for the change in values when running in Python 2 and 3 is that the / operator performs differently depending on the version the program is being run in. This can be read about in PEP 238 which details the change which took place in python 3

To ensure that the same results are achieved in both python 2 and 3, use the following import statement when using python 2:

from __future__ import division

This ensures that your code is compatible with both versions of python.


Post a Comment for "Different Run Results Between Python3 And Python2 For The Same Code"