Pete Houston
Pete Houston

Reputation: 15079

Different results in floating point division

Why is the last digit different in each calculation below? Shouldn't they be the same?

1.0 / 3   # => 0.3333333333333333
10.0 / 3  # => 3.3333333333333335
100.0 / 3 # => 33.333333333333336

Upvotes: 0

Views: 185

Answers (2)

steenslag
steenslag

Reputation: 80065

1.0/3 * 10 == 10.0/3 # => false

Floats are inaccurate, therefore, in current versions of Ruby the use of Rationals is simplified:

1/3r * 10 == 10/3r # => true

Upvotes: 1

spickermann
spickermann

Reputation: 106882

Floating-point numbers cannot precisely represent all real numbers, and floating-point operations cannot precisely represent true arithmetic operations, this leads to many surprising situations.

I advise to read: https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

You can solve most of this problems by using BigDecimal instead of floats

require 'bigdecimal'
BigDecimal.new(  '1.0') / 3    #=>  0.333333333333333333
BigDecimal.new( '10.0') / 3    #=>  3.333333333333333333
BigDecimal.new('100.0') / 3    #=> 33.333333333333333333

Upvotes: 3

Related Questions