Last Updated: February 25, 2016
·
942
· artemeff

Floats

# Who have problems for this?
5.9 - 5.8 # => 0.10000000000000053
# Too easy solution
(5.9 - 5.8).round 14 # => 0.1

Solution and benchmark from comments (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)

2 Responses
Add your response

You can use big decimals if you require arbitrary precision:
require 'bigdecimal'; (BigDecimal.new("5.9") - BigDecimal.new("5.8")).to_f

over 1 year ago ·
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)

But your solution is correct, thanks.

over 1 year ago ·