swy-kw
Last Updated: February 25, 2016
·
789
· artemeff
Diamond logo

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)
Say Thanks
Respond

2 Responses
Add your response

445
Photo on 08.01.2013 at 04.15

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 ·
450
Diamond logo
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 ·