1

Regarding multiplication, behavior of ruby is that 24.0 * 0.1 is not 2.4. Why does this happen?

24.0 * 0.1
# => 2.4000000000000004
sawa
  • 165,429
  • 45
  • 277
  • 381
Faberge eggs
  • 131
  • 7
  • 2
    Floating point math question. There are 100s of duplicates to choose from (but apparently not a good canonical one for Ruby when I just looked). In short, don't worry unless you need absolute precision (e.g. for money) in which case use a different numeric type. – Neil Slater Feb 05 '14 at 13:39
  • 1
    A question downvote is unfair in my opinion as this question is tagged ruby. – Bathsheba Feb 05 '14 at 13:44
  • 2
    This is [a problem every computer scientist should be aware of](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)... – toro2k Feb 05 '14 at 13:46
  • I think the Ruby community on SO should construct a canonical question and answers to this often-asked question. Then closing one like this as a duplicate would be simpler. – Neil Slater Feb 05 '14 at 13:47
  • Thanks for answers. It was my white spot. – Faberge eggs Feb 05 '14 at 14:05
  • Can somebody please explain why this question was downvoted? Sure, this is a topic everybody should know about but everybody had a stage some time when this was all new to him/her. So I don't see a reason why it should not be asked. – Laszlo T Feb 06 '14 at 07:46

1 Answers1

2

Only very few numbers can be represented precisely in floating point arithmetic.

See http://en.wikipedia.org/wiki/Floating_point for more details.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483