Float precision in ruby

前端 未结 2 1800
我在风中等你
我在风中等你 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条回答
  • 2020-12-11 13:26

    In calculation with float you should use sigma method - it means not to compare two values, but compare absolute difference of them with a very little value - 1e-10, for example.

    ((113 * 0.01) - 1.13).abs<1e-10
    
    0 讨论(0)
  • 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")
    => #<BigDecimal:26cefd8,'0.113E1',18(36)> 
    1.9.3p194 :004 > BigDecimal.new("113") * BigDecimal("0.01") == BigDecimal("1.13")
    => true 
    
    0 讨论(0)
提交回复
热议问题