【发布时间】:2018-11-18 05:51:44
【问题描述】:
当我在 python3 中运行这个 python 代码时,它显示了与 python2 不同的结果?为什么会有不同的值?
d = 0
x1 = 0
x2 = 1
y1 = 1
e=125
phi=238
temp_phi = phi
while e > 0:
print (e, temp_phi)
temp1 = temp_phi / e
print (temp1)
temp2 = temp_phi - temp1 * e
print (temp2)
temp_phi = e
e = temp2
x = x2 - temp1 * x1
y = d - temp1 * y1
x2 = x1
x1 = x
d = y1
y1 = y
print (d + phi)
if temp_phi == 1:
print (d + phi)
【问题讨论】:
-
因为
temp1 = temp_phi / e的划分你在里面。 -
当两个操作数都是整数时,
/运算符从整数除法更改为(例如floor(x/y)或(x//y))返回一个浮点数(例如相当于Python2 的float(x)/y) -
除法在 python 2 和 3 中的工作方式不同:stackoverflow.com/questions/21316968/…
标签: python python-3.x variables floating-point iteration