运行这段代码时,我遇到了一个无限循环:
intensity = 0.50
while intensity != 0.65:
print(intensity)
intensity = intensity + 0.05
强度值应该像0.50->0.55->0.60->0.65这样,然后程序就应该退出循环。为什么程序会陷入无限循环呢?
运行这段代码时,我遇到了一个无限循环:
intensity = 0.50
while intensity != 0.65:
print(intensity)
intensity = intensity + 0.05
强度值应该像0.50->0.55->0.60->0.65这样,然后程序就应该退出循环。为什么程序会陷入无限循环呢?
0.65
的值。<
代替!=
(a) :intensity = 0.50
while intensity < 0.65:
print(intensity)
intensity = intensity + 0.05
输出结果如下(同时展示了我所说的不精确性):
0.5
0.55
0.6000000000000001
如果您继续进行,您会看到:
0.6500000000000001
0.7000000000000002
0.7500000000000002
0.8000000000000003
0.65
。(a) 如果下一个值为0.6499...9
而不是0.650..1
,您可能会想知道情况会如何,您可能会很担心。
虽然使用<
会修复后一种情况,但对于前一种情况,您几乎肯定会多进行一次迭代。
您可以通过许多不同的策略来解决这个问题,其中一些策略包括:
round(intensity, 2)
。10-4
(例如)- 类似if abs(intensity - 0.65) < 0.0001
。intensity
初始化为50
,添加5
,与65
进行比较,并打印 round(intensity / 100, 2)
)。Python文档有一个有趣的文章,您可以阅读以进一步了解这些限制。
while True:
if intensity>0.65:
break
print(intensity)
intensity+=0.05
看一下你的输出:
0.5
0.55
0.6000000000000001
0.6500000000000001
0.7000000000000002
0.05无法用有限的二进制浮点数精确表示。
参见is floating point math broken?
如果您想检查该值是否接近,则只需设置所需的公差:
while abs(intensity - 0.65) < my_tolerance:
isclose(intensity, 0.65, rel_tol=1e-9, abs_tol=0.0)
快速编辑您的代码以观察强度如何增加:
from time import sleep
intensity = 0.50
while intensity != 0.65:
print("The intensity is:", intensity)
intensity = intensity + 0.05
sleep(5)
这会导致无限循环,因为 intensity
的值永远不会达到 0.65
输出如下:
The intensity is: 0.5
The intensity is: 0.55
The intensity is: 0.6000000000000001
The intensity is: 0.6500000000000001
The intensity is: 0.7000000000000002
The intensity is: 0.7500000000000002
The intensity is: 0.8000000000000003
The intensity is: 0.8500000000000003
The intensity is: 0.9000000000000004
The intensity is: 0.9500000000000004
The intensity is: 1.0000000000000004
...
...
Process finished with exit code -1
intensity = 0.50
>>> n = 1
>>> while intensity!=0.65 and n<7:
... print(intensity)
... intensity = intensity + .05
... n = n+1
输出
0.5
0.55
0.6000000000000001
0.6500000000000001
0.7000000000000002
0.7500000000000002
while float(str(intensity)[:4])!=0.65 :
... print(intensity)
... intensity = intensity + .05
...
使用python内置的round
函数可以避免这种行为。round
函数将数字四舍五入到指定的小数位精度。
因此,您可以尝试类似于以下的代码:
>>> intensity = 0.50
>>>
>>> while intensity != 0.65:
... print(intensity)
... intensity = round(intensity + 0.05, 2)
0.5
0.55
0.6