???

  • Thread starter Thread starter Kate Luu
  • Start date Start date
K

Kate Luu

Hi all,

does any body know what going on?

float f1 = 0.1f;

float f2 = 0.2f;

f2 - f1 = 0.100000098

if I check the number like this:

if( f2 = = 0.1f )
...............

NEVER WORKED !!!
 
When we use the computer, we need the precisely, absolute accuracy, if the
computer wasn't precise as it is, who will use the computer for
calculation???
don't tell me 2+2=5 is OK for you.

so 0.2 - 0.1 should be = 0.1, not 0.1000000098;

For scientific, we need the result must be very accurate.

0.2 - 0.1 is nothing. Do you ever work for government???
 
If i'm not mistaken, you can't get .1 exactly with floating point numbers
because of the way they are implemented, right?

<quote from http://grouper.ieee.org/groups/754/faq.html>
Binary floating-point numbers consist of signed integers multiplied by
powers of two. When fractional, you can also consider them as integers
divided by powers of two. The decimal number 0.1, or 1/10, is not an integer
over a power of two. The 854 standard encompasses decimal arithmetic, but
there is little hardware support outside of desktop calculators.

</quote>
 
Thanks you so much, Tickle and Time. I'm very appreciated for you all help.
Have a nice day...

Kate Luu
 
If only that were true

float isn't very precise, and banks use decimal numbers that are many times
more precise than float, but they aren't very precise either.

I think someone mentioned that rounding 5.555 didn't lead to 5.56 but 5.55,
because the nearest number to 5.555 was in fact
5.554999999999999214916394143 or something. So you can't use exact
comparison for floats.

Try turning it into an int by multiplying the numbers with 10000 or
something and check if (int)(f2*10000) == 1000
 
Back
Top