0

Why does

2.23 + 0.17 = 2.4899999999999998

but

1.23 + 0.17 = 1.49

Screenshot from a debug console:

enter image description here

Charles
  • 50,943
  • 13
  • 104
  • 142
artwl
  • 3,502
  • 6
  • 38
  • 53
  • 8
    [What every programmer should know about floting point arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Fildor Oct 14 '13 at 06:46
  • The same reason 1/3 * 3 = .9999999 in fixed-digit decimal. The nature of fixed precision representations is that they, at least sometimes, return approximate results. – David Schwartz Oct 14 '13 at 06:47
  • 1
    There are many identical questions. I don't have any of them bookmarked but the word to search would probably be "IEEE754". – Denys Séguret Oct 14 '13 at 06:47

1 Answers1

4

It's the magic of floating point numbers :)

You can read some interesting things about it. Also, there are solutions for that problems.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Elon Than
  • 9,603
  • 4
  • 27
  • 37