Possible Duplicate:
problem in comparing double values in C#
I’ve read it elsewhere, but really forget the answer so I ask here again. This loop seems never end regardless you code it in any language (I test it in C#, C++, Java…):
double d = 2.0;
while(d != 0.0){
d = d - 0.2;
}
Floating point calculations are not perfectly precise. You will get a representation error because 0.2 doesn’t have an exact representation as a binary floating point number so the value doesn’t become exactly equal to zero. Try adding a debug statement to see the problem:
One way to solve it is to use the type
decimal.