为什么浮点上的简单数学运算会在 VB.Net 和 Python 中返回意外(不准确)的结果?

2022-01-09 00:00:00 python floating-point vb.net

问题描述

x = 4.2 - 0.1  

vb.net 给出 4.1000000000000005
python给出4.1000000000000005

Excel给出4.1
Google calc 给出 4.1

Excel gives 4.1
Google calc gives 4.1

发生这种情况的原因是什么?

What is the reason this happens?


解决方案

浮点/双精度.

您必须记住,在二进制中,4.1 = 4 + 1/10.1/10 是二进制的无限重复和,就像 1/9 是十进制的无限和.

You must remember that in binary, 4.1 = 4 + 1/10. 1/10 is an infinitely repeating sum in binary, much like 1/9 is an infinite sum in decimal.

相关文章