0

Possible Duplicates:
Precision of Floating Point
Floating point arithmetic is too reliable.

Hi Guys,

I came across a rather strange looking problem, i am running a loop from 82.01 to 169.06 in steps of 0.01 but when i reach 128.01 and do (128.01+0.01) it gives 128.019999999998 instead of 128.02. I am using double for all these computations. If i use decimal to do these computations it works out fine, am i missing a very basic funda here, i found some articles and discussions on the web explaining that decimal is the correct data type to do these computations but still a basic computation like (128.01+0.01) should give correct results.

Community
  • 1
  • 1
DotNetJourneyMen
  • 129
  • 1
  • 2
  • 8
  • 7
    Then you are expecting too much. – BoltClock Feb 17 '11 at 14:41
  • 1
    The exact same question has just been asked and answered a few minutes ago: http://stackoverflow.com/questions/5029955/decimal-rounding-is-off-for-276-304304/5029982#5029982 Use the search next time first, please. – Daniel Hilgarth Feb 17 '11 at 14:42
  • This is to be expected (given a valid expression). Not all floating point numbers can be represented exactly in binary so there will be rounding errors in calculations. You will get different rounding errors for `decimal` and `double` as their bit representations are different. This is also a duplicate question. – ChrisF Feb 17 '11 at 14:42
  • See http://floating-point-gui.de/ for a good explanation. – Sven Marnach Feb 17 '11 at 14:43
  • Possible duplicate of http://stackoverflow.com/questions/5029955/decimal-rounding-is-off-for-276-304304/5029982#5029982 – Daniel Hilgarth Feb 17 '11 at 14:46
  • In this case, you might be better off looping from 8201 to 16906 using an integer type and then casting to a float or double as needed. The error will still exist in the floating point representation, but it won't accumulate to make things worse with each iteration of your loop. – andand Feb 17 '11 at 15:15

2 Answers2

0

Floating-point calculations are inherently inaccurate, so, this behaviour is completely normal. If you really need higher accuracy, stick to decimals, but keep in mind that they are way, way slower than floats/doubles. Usually, it's better to just accept the inaccuracies and round as needed.

Creshal
  • 493
  • 1
  • 4
  • 15
0

Here is the scientific detailed explanation of your issue

Mathieu
  • 4,449
  • 7
  • 41
  • 60