Reputation: 1375
I just encountered this and can't figure out why exactly Ruby behaves this way.
Could someone explain why in Ruby:
6.84 - 3.6 == 3.2399999999999998
and not just 3.24? I know that it's related to the binary representation of those number and that
(Decimal('6.84') - Decimal('3.6'))
would return the expected results. I'm just curious about the detailed explanation of the Float behavior.
Upvotes: 1
Views: 331
Reputation: 15920
See the answers in "Why is the 100*0.07 equal to 6.9999....?" (and many other places) about floating-point math.
Why is the 100*0.07 equal to 6.9999....?
Upvotes: 3
Reputation: 211610
Floating point uses an internal representation that's inherently imprecise. You should always round down your answers for display purposes:
'%.4f' % (6.84 - 3.6)
# => "3.2400"
Left to its own devices, Ruby, like many other languages, will express floating point numbers to a ridiculous degree of precision.
Upvotes: 0
Reputation: 635
Float/Double is not that precise in most cases (because floats interpolate numbers with base2). The result of floating point operations may vary on different CPU's. The area close to .01 is more precise than the area close to .99
If you need to compare values calculated with float operations use an epsilon value (really small value) in order to erase rounding errors.
Pseudocode:
boolean equals(float x, float y) {
float result = x - y;
if(result*result < epsilon)
return true;
else
false;
}
Upvotes: 1
Reputation: 621
0.01 or 0.07 cannot be precisely represented in binary floating-point.
Upvotes: 0
Reputation: 912
Because double/float use base-2 notation and decimal base-10 notation.
Here's a useful link: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Upvotes: 3