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

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

问题描述

x = 4.2 - 0.1  

vb.net给出 4.1000000000000005

python给出 4.1000000000000005

vb.net gives 4.1000000000000005
python gives 4.1000000000000005

Excel给出 4.1

Google calc 给出 4.1

Excel gives 4.1
Google calc gives 4.1

这是什么原因? / p>

What is the reason this happens?

推荐答案

Float / double precision。

您必须记住,在二进制中,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天全站免登陆