0

Possible Duplicate:
Why 6.84 - 3.6 == 3.2399999999999998

And by extension, why does 49.99999999999999 * 1.1 equal 55.0?

I assume this is do to with floating point arithmetic, but am somewhat perplexed as to why this occurs with such a simple sum, and why it is also true for the multiplication case.

Community
  • 1
  • 1
djlumley
  • 2,955
  • 2
  • 24
  • 30

2 Answers2

4

You're correct, it is entirely to do with floating point arithmetic. Many decimal numbers are only representable to a certain accuracy in binary, which is why you see the behaviour here. This isn't restricted to ruby - I'd suggest reading What every computer scientist should know about floating point arithmetic.

Yuushi
  • 25,132
  • 7
  • 63
  • 81
3

Your computer works in binary, not in decimal. The number 1.1 cannot be exactly represented in a finite binary representation, so it is necessarily an approximation.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285