Float parsing

  • Thread starter Thread starter Rene
  • Start date Start date
R

Rene

The following instruction:

float myFloat = float.Parse("30.900665");

Assigns a value to myFloat of "30.9006653", how can this be? Where did the
extra 3 came from?

Thanks
 
Rene said:
The following instruction:

float myFloat = float.Parse("30.900665");

Assigns a value to myFloat of "30.9006653", how can this be? Where did the
extra 3 came from?

What Every Computer Scientist Should Know About Floating-Point
Arithmetic:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

Basically, floating-point numbers are stored in the computer as floating
point binary numbers, not floating point decimal numbers; as such, the
parts after the decimal point, in order to match exactly, must be a sum
of negative powers of two. For example:

0.5 is exactly representable, because it's 2^-1
0.25 is exactly representable, because it's 2^-2
0.75 is exactly representable, because it's 2^-1 + 2^-2
0.1 is *not* exactly representable as a binary number without
introducing infinitely repeating digits, and so will differ from the
decimal number 0.1 by some small amount.

-- Barry
 
Rene said:
The following instruction:

float myFloat = float.Parse("30.900665");

Assigns a value to myFloat of "30.9006653", how can this be? Where did the
extra 3 came from?

try using the decimal data type instead. If you're using anything that must
be completely accurate (eg money) then you should not use floating point in
any part of your app (eg, database or code) and you should be careful not to
convert from decimal to floating point and back. If you use floating points
you need to consider the results to not be completely accurate and that some
of the least significant digits can be incorrect or lost, eg this second
messagebox shows zero:

float x = 0.000001f;
MessageBox.Show(x.ToString());
x = x + 100;
x = x - 100;
MessageBox.Show(x.ToString());

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top