Thursday, July 22, 2004

I was having a discussion on MSDN Newsgroup in response to a post that said that simple arithmetic in VS.NET command window yields wrong results.

?11075.04+1814.20
12889.2484

was the result (and this is what happens!)  I have seen this problem before though I can't recollect exactly where.

Code like this:

double x = 11075.04+1814.20;

MessageBox.Show(x.ToString());

if(x == 1468.62) { MessageBox.Show("true"); } else { MessageBox.Show("false"); }

will show the expected 12889.24 as the first messagebox result and then show false!  The if condition basically does a bit-wise comparison and hence fails (since the actual answer is 12889.2484)

The problem has its roots in how computers are designed to handle floating point numbers.

Jon's homepage - a wonderful resource for some very interesting articles - has a complete description of the issue and the reasons behind it:  http://www.yoda.arachsys.com/csharp/floatingpoint.html