如何比较python中的舍入浮点数?

时间:2018-08-14 12:40:22

标签: python

我有两个浮点变量,例如:

prediction_value=0.45889889989988
labels_value=0.458898899899087

然后在这两个变量上使用math.round:

math.round(prediction_value,2)
math.round(labels_value,2)

然后,我想比较两个变量,并期望获得一个带有“ true”的布尔值:

if math.round(prediction_value,2)==math.round(labels_value,2):
....

很遗憾,我得到了 false 。 我不知道为什么会得到这个奇怪的结果,有人知道比较两个四舍五入的float变量的合理方法吗?

pic1

pic2

pic3

0 个答案:

没有答案