为什么简单的浮点数学运算在 VB.Net 和 Python 中返回意外(不准确)的结果? [英] Why do simple math operations on floating point return unexpected (inaccurate) results in VB.Net and Python?

查看:18
本文介绍了为什么简单的浮点数学运算在 VB.Net 和 Python 中返回意外(不准确)的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于为什么简单的浮点数学运算在 VB.Net 和 Python 中返回意外(不准确)的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆