Last Updated: February 25, 2016
·
902
· 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)``````

153.3K
9

135.6K
0

### Take a photo of yourself every time you commit

108.5K
28

#### 2 Responses

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 ·

#### Have a fresh tip? Share with Coderwall community!

Best #Ruby Authors
mcansky
130.2K
knoopx
109.8K
stevennunez
95.53K
Related Tags
#ruby
#float
Filed Under

Awesome Job