Thursday, August 25, 2005

Floating-Point Numbers

I have been working with floating-point numbers. Floating-point numbers are not exact in computers, they are numbers that closely represent a decimal number.

See the following URL’s for more information floating-point numbers.
www.lahey.com/float.htm
www.codeproject.com/dotnet/ExtremeFloatingPoint1.asp
www2.hursley.ibm.com/decimal/decifaq1.html

Here are some things that I found quite surprising.

1) In Microsoft Excel, enter the following into a cell.

=(0.5 - 0.4 - 0.1)

The result should be 0, however it is actually -2.77556E-17.

2) I created a C# windows applications and dropped a button onto the form and then added the following code to the button’s click event.

double f = Convert.ToDouble("1000000000.12345678901");
MessageBox.Show(f.ToString("0.0000000000000000"));
f = f - 1000000000;
MessageBox.Show(f.ToString("0.0000000000000000"));

When you run the application you will get the following message boxes:

1) 1000000000.1234600000000000
2) 0.1234568357467650

You would expect that the number displayed in the second message box would be 0.12346 because that is what the fractional part is after the initial assignment of the f variable. But it turns out that after you subtract 1 billion, something happens to cause the numbers at the end to appear.

No comments: