1

Possible Duplicate:
Is JavaScript’s Floating-Point Math Broken?

This is going to be a very rudimentary comp-sci question. Consider the following C# (the same holds true for JS, I assume it's how math works with doubles in general).

var i = .01;
i+=.01; //i=.02
i+=.01; //i=.03
i+=.01; //i=.04
i+=.01; //i=.05
i+=.01; //i=.0600000000005 (I may have added/missed a few 0s in there)
i+=.01; //i=.07
i+=.01; //i=.08
i+=.01; //i=.09
i+=.01; //i=.0999999999992 (I may have added/missed a few 9s in there)

So, what's happening and how can I accurately predict the sum of i+.01?

Community
  • 1
  • 1
Billdr
  • 1,557
  • 2
  • 17
  • 30
  • 2
    Read this and see if it helps: http://csharpindepth.com/Articles/General/FloatingPoint.aspx – Jon Skeet Dec 25 '12 at 15:11
  • 2
    What problem are you actually trying to solve? If the decimal value of low significance digits matter to you, you should not use `double` in the first place. Use `decimal` or similar constructs. – CodesInChaos Dec 25 '12 at 15:12
  • @CodesInChaos My goal was to get every hundredth interval between 0 and x. I worked around it by doing some division, but I want to understand how math works with doubles. JonSkeet, taking a look now. Thanks. – Billdr Dec 25 '12 at 15:15
  • This probably is a duplicate, I just couldn't come up with a way to phrase it to find a result that explained it. Jon Skeet's link got me going. – Billdr Dec 25 '12 at 15:20

1 Answers1

0

A double isn't just a way to get decimal points for your number. It uses floating point math. This article by Pete Becker does a good job of explaining how that works.

Billdr
  • 1,557
  • 2
  • 17
  • 30