为什么 0.1 + 0.2 结果为 0.30000000000000004

为什么 0.1 + 0.2 结果为 0.30000000000000004

为什么 0.1 + 0.2 结果为 0.30000000000000004

如果你打开浏览器的控制台,输入以下代码并运行

 > 0.1 + 0.2
 0.30000000000000004
奇怪,为什么不是 0.3。

这时可能你会去搜一下(很容易搜到结果),但是如果你了解计算机是如何存储小数(准确地说是浮点数)的话,很容易推断出原因。

我们需要有以下基础知识:

  1. 计算机将所有数据以二进制的形式存储
  2. 计算机用有限的大小来存储数据(因为现实生活中不存在无限大的内存或硬盘)
    好的,然后结合我们的问题来看。

计算机如何存储 0.1 和 0.2?

如果你对十进制转二进制有兴趣可以看下图:

如果看不懂也可以直接看结论:十进制的 0.1 转为二进制,得到一个无限循环小数:0.00011…。
也就是说,二进制无法「用有限的位数」来表示 0.1。对于 0.2 也是一样的,不赘述。二进制能「用有限的位数」表示的有:0.5、0.25、0.125 等。

但是计算机只能用有限的位数来存一个数,所以最终,计算机存的数是一个近似于 0.1 的小数。

确切的二进制值是0-01111111011-1001100110011001100110011001100110011001100110011010,
对应的十进制值是0.10000000000000000555…。

这是计算机能表示的最接近 0.1 的数字(存储一个数字的空间越大则越精确)。(具体转换过程参考这里

所以当我们计算 0.1 + 0.2 时,实际上算的是两个近似值相加,得到的值当然也是近似等于 0.3。

总结

  1. 问题的根源是十进制小数转为二进制小数的过程中,会损失精度
  2. 你在写代码的过程中,遇到小数都要小心,比如下面的代码会造成死循环
var i = 0.1
while(i!=1){
    console.log(i)
    i += 0.1
}
  1. 因为 i 加 9 次 0.1,得到的值是 1 的近似值,并不是 1。

你应该对计算机存储方式有一定的了解。

作者:方应航、方方老师 https://zhuanlan.zhihu.com/p/22328524

28 thoughts on “为什么 0.1 + 0.2 结果为 0.30000000000000004”

  1. Wonderful article! This is the kind of information that are supposed to be shared across the net. Shame on the seek engines for no longer positioning this put up higher! Come on over and visit my web site . Thanks =)|

  2. Heya! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no backup. Do you have any solutions to prevent hackers?|

  3. I just couldn’t depart your site before suggesting that I actually loved the standard information an individual supply to your guests? Is gonna be again continuously in order to check up on new posts|

  4. Hello there, I discovered your blog by the use of Google while searching for a similar matter, your web site came up, it looks great. I have bookmarked it in my google bookmarks.

  5. Fantastic goods from you, man. I’ve understand your stuff previous to and you’re just extremely fantastic. I really like what you’ve acquired here, certainly like what you’re saying and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I cant wait to read far more from you. This is really a tremendous site.|

  6. Hi! I simply would like to offer you a big thumbs up for your excellent information you have right here on this post. I will be returning to your website for more soon.|

  7. I blog quite often and I seriously appreciate your information. Your article has really peaked my interest. I’m going to book mark your site and keep checking for new information about once per week. I subscribed to your RSS feed too.|

  8. Hi exceptional website! Does running a blog similar to this take a large amount of work? I have very little understanding of coding but I was hoping to start my own blog soon. Anyway, should you have any recommendations or techniques for new blog owners please share. I know this is off topic however I simply needed to ask. Many thanks!|

  9. excellent publish, very informative. I’m wondering why the other specialists of this sector do not understand this. You should proceed your writing. I’m sure, you have a great readers’ base already!|

  10. Just desire to say your article is as surprising. The clearness in your post is just nice and i can assume you’re an expert on this subject. Fine with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the rewarding work.|

  11. You’re so cool! I don’t believe I have read anything like that before. So great to find another person with genuine thoughts on this subject matter. Really.. thank you for starting this up. This web site is one thing that’s needed on the web, someone with a little originality!|

  12. Excellent post however I was wanting to know if you could write a litte more on this subject? I’d be very grateful if you could elaborate a little bit further. Thank you!|

  13. Thanks for another informative web site. Where else could I am getting that kind of info written in such an ideal means? I have a mission that I’m simply now running on, and I have been at the look out for such information.|

若愚@饥人谷进行回复 取消回复

电子邮件地址不会被公开。