Float precision in ruby

前端 未结 2 1807
我在风中等你
我在风中等你 2020-12-11 13:01

I\'m writing a ruby program that uses floats. I\'m having trouble with the precision. For example

1.9.3p194 :013 > 113.0 * 0.01
# => 1.13000000000000         


        
2条回答
  •  猫巷女王i
    2020-12-11 13:30

    This is an inherent limitation in floating point numbers (even 0.01 doesn't have an exact binary floating point representation). You can use the technique provided by Aleksey or, if you want perfect precision, use the BigDecimal class bundled in ruby. It's more verbose, and slower, but it will give the right results:

    require 'bigdecimal'
    => true
    1.9.3p194 :003 > BigDecimal.new("113") * BigDecimal("0.01")
    => # 
    1.9.3p194 :004 > BigDecimal.new("113") * BigDecimal("0.01") == BigDecimal("1.13")
    => true 
    

提交回复
热议问题