Bug in conversion to ingegers???

  • Thread starter Thread starter Vladimir
  • Start date Start date
V

Vladimir

Can anybody say me why...

/*
int i;
float f;

// Result is one.
f = 100 * 0.01f;
i = (int)f;
MessageBox.Show(i.ToString());

// Result is zero!!!
i = (int)(100 * 0.01f);
MessageBox.Show(i.ToString());
*/
 
0.01F cannot be exactly represented as a floating point number. It is
actually equal to something like .00999999999etc. So, when you multiply it
by 100, you get .99999999999etc

As far as I can tell, the multiplcation is done using doubles, so
assigning that double to a float has it rounded to the nearest value in the
float range, which in this case, is 1.

However, when you cast it directly to an int, the decimal portion is
truncated, before it can be rounded up.

To demostrate this, change the line in the second block to

i = (int)(float) (100.0 * 0.01f);

And you will get the results you expect.

Just another of the reasons why floating point is evil.
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 

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