Different Run Results Between Python3 And Python2 For The Same Code
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"