# Who have problems for this?
5.9 - 5.8 # => 0.10000000000000053
# Too easy solution
(5.9 - 5.8).round 14 # => 0.1
Solução e benchmark de comentários (thx to sheerun):
require "benchmark"
require "bigdecimal"
n = 1_000_000
Benchmark.bm do |x|
x.report { for i in 1..n; (5.9 - 5.8).round 14; end }
x.report { for i in 1..n; (BigDecimal.new("5.9") - BigDecimal.new("5.8")).to_f; end }
end
user system total real
0.240000 0.000000 0.240000 ( 0.241408)
4.140000 0.000000 4.140000 ( 4.139652)