为什么 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

174 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.|

  14. We stumbled over here by a different page and thought I might as well check things out. I like what I see so now i’m following you. Look forward to checking out your web page again.|

  15. Excellent goods from you, man. I have understand your stuff previous to and you’re just too magnificent. I actually like what you have acquired here, certainly like what you’re saying and the way in which you say it. You make it entertaining and you still care for to keep it smart. I can not wait to read far more from you. This is actually a terrific site.|

  16. I have been exploring for a little bit for any high-quality articles or weblog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this website. Reading this information So i’m happy to convey that I’ve an incredibly good uncanny feeling I found out just what I needed. I most indubitably will make sure to do not forget this website and give it a glance on a continuing basis.|

  17. Does your website have a contact page? I’m having a tough time locating it but, I’d like to send you an e-mail. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.|

  18. Good web site! I really love how it is simple on my eyes and the data are well written. I’m wondering how I could be notified when a new post has been made. I have subscribed to your feed which must do the trick! Have a nice day!

  19. Good web site! I really love how it is simple on my eyes and the data are well written. I’m wondering how I could be notified when a new post has been made. I have subscribed to your feed which must do the trick! Have a nice day!

  20. certainly like your web site however you need to test the spelling on quite a few of your posts. Many of them are rife with spelling issues and I in finding it very troublesome to inform the truth then again I¡¦ll certainly come again again.

  21. Wow! At last I got a website from where I be capable of actually take useful data regarding my study and knowledge.

  22. Excellent blog here! Also your site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol

  23. I don’t even know how I ended up here, but I thought this post was great. I don’t know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!

  24. It is actually a great and useful piece of info. I am glad that you shared this helpful info with us. Please stay us informed like this. Thank you for sharing.|

  25. Cool blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog stand out. Please let me know where you got your design. Bless you|

  26. Hello just wanted to give you a brief heads up and let you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different browsers and both show the same results.|

  27. Hi, I do believe this is an excellent web site. I stumbledupon it 😉 I am going to come back yet again since i have saved as a favorite it. Money and freedom is the greatest way to change, may you be rich and continue to help others.|

  28. Unquestionably believe that which you stated. Your favourite justification seemed to be at the net the easiest factor to be mindful of. I say to you, I certainly get annoyed whilst other people consider worries that they plainly do not recognize about. You managed to hit the nail upon the top and outlined out the entire thing without having side-effects , other people could take a signal. Will likely be again to get more. Thank you|

  29. I don’t even know how I ended up here, but I thought this post was great. I don’t know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!

  30. Great blog here! Also your web site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol|

  31. For most up-to-date news you have to go to see web and on world-wide-web I found this website as a most excellent site for most recent updates.|

  32. It’s appropriate time to make some plans for the future and it’s time
    to be happy. I’ve read this post and if I could I desire to suggest you few
    interesting things or advice. Maybe you can write next articles referring to this
    article. I wish to read more things about it!

  33. I’ve read several good stuff here. Certainly worth
    bookmarking for revisiting. I wonder how a lot attempt you put to make any such great
    informative site.

  34. After I initially left a comment I appear to have clicked on the -Notify me when new comments are
    added- checkbox and from now on every time a comment
    is added I get four emails with the same comment. There
    has to be a way you are able to remove me from that service?

    Cheers!

  35. I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to construct my own blog and would like to find out where u got this from. thanks a lot|

  36. Wow, that’s what I was exploring for, what a data!

    present here at this webpage, thanks admin of this web site.

  37. Quality content is the important to be a focus for the visitors to go to see the web page, that’s what this web site is providing.

  38. It’s actually a great and useful piece of info. I
    am happy that you shared this helpful information with us.

    Please stay us informed like this. Thank you for sharing.

  39. Amazing blog! Do you have any hints for aspiring writers? I’m planning to start my own site soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally confused .. Any suggestions? Thanks!|

  40. You really make it appear so easy together with your presentation but I find this topic to be actually something that I think I would by no means understand. It sort of feels too complicated and extremely huge for me. I’m taking a look forward for your subsequent post, I’ll try to get the dangle of it!|

  41. With havin so much written content do you ever run into any problems of plagorism or copyright violation? My site has a lot of exclusive content I’ve either written myself or outsourced but it looks like a lot of it is popping it up all over the internet without my agreement. Do you know any solutions to help protect against content from being stolen? I’d certainly appreciate it.|

  42. Excellent blog here! Also your web site lots up fast! What host are you the usage of? Can I get your associate link on your host? I want my web site loaded up as quickly as yours lol|

  43. Hi! I could have sworn I’ve been to this site before but after browsing through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!|

  44. I do believe all the ideas you have presented for your post. They are very convincing and will definitely work. Nonetheless, the posts are very brief for starters. May just you please prolong them a bit from subsequent time? Thanks for the post.|

  45. I don’t even know how I ended up here, but I thought this post was great. I don’t know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!

  46. It is appropriate time to make some plans for the future and it’s time to be happy. I have read this post and if I could I want to suggest you few interesting things or advice. Maybe you could write next articles referring to this article. I desire to read even more things about it!|

  47. I loved as much as you will receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this hike.|

  48. Thank you for the good writeup. It actually used to be a enjoyment account it. Look complicated to more introduced agreeable from you! However, how can we keep in touch?|

  49. Hello there I am so delighted I found your blog, I really found you by accident, while I was looking on Askjeeve for something else, Anyhow I am here now and would just like to say thanks a lot for a remarkable post and a all round entertaining blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb b.|

  50. I’m truly enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Outstanding work!|

  51. May I simply say what a relief to discover a person that really understands what they’re discussing on the web. You actually realize how to bring an issue to light and make it important. More people really need to check this out and understand this side of the story. It’s surprising you’re not more popular given that you certainly have the gift.|

  52. Fantastic post but I was wanting to know if you could write a litte more on this topic? I’d be very grateful if you could elaborate a little bit further. Bless you!|

  53. I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to create my own blog and would like to find out where u got this from. cheers|

  54. Hey There. I discovered your blog the usage of msn. That is a very smartly written article. I will make sure to bookmark it and come back to learn more of your helpful info. Thank you for the post. I’ll certainly comeback.|

  55. Neat blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. With thanks|

  56. Good day! This is my 1st comment here so I just wanted to give a quick shout
    out and say I really enjoy reading through your blog posts.
    Can you suggest any other blogs/websites/forums that go over the
    same subjects? Thank you so much!

  57. Pretty component of content. I simply stumbled upon your web site and in accession capital to assert that I acquire in fact loved account your weblog posts.
    Any way I will be subscribing for your feeds and even I achievement you access
    consistently quickly.

  58. Today, I went to the beach front with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!|

  59. Hmm is anyone else experiencing problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.|

  60. May I just say what a comfort to uncover someone who truly
    understands what they are talking about on the internet.
    You actually realize how to bring an issue to light
    and make it important. A lot more people really need to read this and understand this side of
    your story. I can’t believe you’re not more popular because
    you most certainly possess the gift.

  61. Usually I do not read post on blogs, but I wish to say that this write-up very compelled me to take a look at and
    do so! Your writing taste has been surprised
    me. Thank you, very great post.

  62. After I initially commented I appear to have clicked the -Notify me when new comments are added- checkbox and now whenever a comment is added I recieve four emails with the exact same comment. Is there a way you can remove me from that service? Kudos!|

  63. Howdy just wanted to give you a quick heads up. The text in your content seem
    to be running off the screen in Safari. I’m not
    sure if this is a formatting issue or something to do with web
    browser compatibility but I thought I’d post to let you know.
    The design and style look great though! Hope you get the problem solved soon. Cheers

  64. Greetings from Los angeles! I’m bored to death at work so I decided to check out your website on my iphone during lunch break. I enjoy the info you present here and can’t wait to take a look when I get home. I’m amazed at how fast your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyways, great blog!|

  65. Hi would you mind letting me know which webhost you’re utilizing? I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot quicker then most. Can you recommend a good web hosting provider at a fair price? Cheers, I appreciate it!|

  66. Hey there! I understand this is somewhat off-topic but I had to ask. Does running a well-established website like yours require a large amount of work? I’m brand new to writing a blog however I do write in my journal on a daily basis. I’d like to start a blog so I will be able to share my experience and thoughts online. Please let me know if you have any recommendations or tips for brand new aspiring bloggers. Thankyou!|

  67. Hey there! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my apple iphone. I’m trying to find a theme or plugin that might be able to fix this issue. If you have any suggestions, please share. Thanks!|

  68. Hey there I am so delighted I found your blog, I really found you by mistake, while I was researching on Aol for something else, Nonetheless I am here now and would just like to say cheers for a fantastic post and a all round interesting blog (I also love the theme/design), I don’t have time to read it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the excellent work.|

  69. Hello there! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading through your articles. Can you suggest any other blogs/websites/forums that go over the same topics? Many thanks!|

  70. Hi, i think that i saw you visited my site so i came to “return the favor”.I am attempting to find things to improve my website!I suppose its ok to use some of your ideas!!|

  71. I have not checked in here for some time because I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my everyday bloglist. You deserve it my friend 🙂

  72. I’ve been surfing on-line more than 3 hours nowadays, but I never discovered any interesting article like yours. It’s beautiful price sufficient for me. Personally, if all website owners and bloggers made good content as you did, the internet will probably be a lot more useful than ever before.|

  73. Hey are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and create my own. Do you require any coding expertise to make your own blog? Any help would be greatly appreciated!

  74. Hey there, I think your site might be having browser compatibility issues. When I look at your blog in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, great blog!|

  75. Greetings from Ohio! I’m bored to death at work so I decided to check out your blog on my iphone during lunch break. I really like the info you provide here and can’t wait to take a look when I get home. I’m surprised at how quick your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyways, superb blog!|

  76. I’ve been browsing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. Personally, if all web owners and bloggers made good content as you did, the web will be much more useful than ever before.|

  77. Woah! I’m really loving the template/theme of this website. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between superb usability and appearance. I must say you’ve done a very good job with this. Also, the blog loads extremely fast for me on Internet explorer. Outstanding Blog!|

  78. Woah! I’m really loving the template/theme of this site. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and appearance. I must say you’ve done a very good job with this. In addition, the blog loads extremely quick for me on Opera. Superb Blog!|

  79. Hey just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Firefox. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I figured I’d post to let you know. The style and design look great though! Hope you get the problem solved soon. Kudos|

  80. Hey there would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker then most. Can you suggest a good hosting provider at a reasonable price? Many thanks, I appreciate it!|

  81. It’s appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I desire to suggest you few interesting things or advice. Perhaps you could write next articles referring to this article. I want to read even more things about it!|

  82. Does your site have a contact page? I’m having trouble locating it but, I’d like to send you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it expand over time.|

  83. Amazing blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your theme. Thanks|

  84. My partner and I stumbled over here coming from a different website and thought I should check things out. I like what I see so i am just following you. Look forward to going over your web page for a second time.|

  85. Hello would you mind letting me know which hosting company you’re utilizing? I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot quicker then most. Can you suggest a good internet hosting provider at a honest price? Thanks, I appreciate it!|

  86. Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Firefox. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought I’d post to let you know. The style and design look great though! Hope you get the problem fixed soon. Many thanks|

  87. I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks!|

  88. I am curious to find out what blog system you have been working with? I’m experiencing some small security issues with my latest website and I would like to find something more risk-free. Do you have any suggestions?|

  89. Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!|

发表评论

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