-1

While in middle of an execution,

I just opened the immediate window and i typed like this,

?1.66 + 0.83

so as a result of this expression the following was displayed,

2.4899999999999998

Actually that expression should have returned 2.49 but it is returned the same value with different floating points. What is wrong with this.? Is there any work around available to fix this.? by the way i don't want to format the above value.

tezzo
  • 10,858
  • 1
  • 25
  • 48
Sathish
  • 4,419
  • 4
  • 30
  • 59
  • 6
    Welcome to the wonderful world of [floating point precision](http://en.wikipedia.org/wiki/Floating_point) – Steve Oct 08 '13 at 14:13
  • 3
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – dtb Oct 08 '13 at 14:13
  • [Is JavaScript floating point math broken?](http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken?rq=1) – Steve Oct 08 '13 at 14:14
  • [Is double Multiplication Broken in .NET?](http://stackoverflow.com/questions/1420752/is-double-multiplication-broken-in-net) [odd math in actionscript 3?](http://stackoverflow.com/questions/784594/odd-math-in-actionscript-3]) [weird math calculation](http://stackoverflow.com/questions/11771718/weird-math-calculation) [Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?](http://stackoverflow.com/questions/177506/why-do-i-see-a-double-variable-initialized-to-some-value-like-21-4-as-21-3999996) – sloth Oct 08 '13 at 14:40
  • [Why do floats seem to add incorrectly in Java?](http://stackoverflow.com/questions/19250097/is-vb-net-math-broken) [Floating point arithmetic not producing exact results in Java](http://stackoverflow.com/questions/1661273/floating-point-arithmetic-not-producing-exact-results-in-java) --- You see, you're not the first one with this problem... – sloth Oct 08 '13 at 14:41

2 Answers2

14

This is just how floating numbers work. They work with some level of precision, when you write 1.66 it might actually be stored as 1.66000000000000000001 or 1.65999999999999999 in memory. So when you start adding these numbers together that small error adds up.

If you want to avoid this, you could use System.Decimal data type which will solve this issue most scenarios (it uses a different model to store the value in memory that preserves the exact value).

For example, try

?1.66D + 0.83D

D suffix means that the value is of type Decimal

Knaģis
  • 20,827
  • 7
  • 66
  • 80
  • 1
    This is better than many answers which say, without qualification, that `Decimal` is exact. But is “most scenarios” credible? Dividing a bill three ways? Compound interest? Scientific functions (log, sin)? Echo filter? – Eric Postpischil Oct 08 '13 at 14:42
  • "This is just how floating pint numbers work" *because they are actually being converted to binary* – peterG Oct 08 '13 at 15:53
0

The immediate window is displaying the value to decimal places, which is its default action, formatting needs to be applied if you want 2 decimal places, rounded up.

DarrylGodden
  • 1,526
  • 5
  • 25
  • 44