Reputation: 954
I'm having problems working with BigDecimal in Rails 3.2.3 and ruby 1.9.3p362
the following sequence of number comparisons output absurd results
1.9.3-p362 :060 > b = BigDecimal.new('620.56')
=> #<BigDecimal:68665e0,'0.62056E3',18(18)>
1.9.3-p362 :061 > b <= 620.56
=> false
1.9.3-p362 :062 > b > 620.56
=> true
1.9.3-p362 :063 > (b - 620.56) > 0
=> false
1.9.3-p362 :064 > (b - 620.56) == 0
=> true
in other words, this is telling that:
B > A
and
B - A == 0
what am I missing ?
Upvotes: 1
Views: 1056
Reputation: 15515
Welcome to the wonderful world of Floating Point Arithmetic.
This website gives a good introduction of how and why (check the Basic Answers first), but what it comes down to is:
Computer are not very accurate when it comes to floating point numbers. Rounding errors are everywhere, so you have to be careful what you do.
Upvotes: 1