Addition error with ruby-1.9.2 [duplicate]

别说谁变了你拦得住时间么 提交于 2019-11-30 20:16:21

You need bigdecimal for this to make work.

(BigDecimal('0.1') + BigDecimal("0.2")).to_f

See below link:

http://redmine.ruby-lang.org/issues/4394

Your old ruby lied to you:

$ ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [x86_64-linux]
$ irb
irb(main):001:0> printf("%40.40f\n", 0.1 + 0.2)
0.3000000000000000444089209850062616169453
=> nil

Floating point numbers are very tricky beasts.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!