On Thu, Oct 22, 2009 at 9:46 PM, Okan <[email protected]> wrote:
> hello all,
>
> why the output is "f is NOT 1.0" ?
>
> /****************************/
> #include <stdio.h>
>
> int main()
> {
>        float f = 0.0f;
>    int i;
>
>    for (i = 0; i < 10; i++)
>        f += 0.1f;

Because you aren't adding exactly 0.1 every time round the loop
because 0.1 cannot be represented exactly in binary, in much the same
way 1/3 cannot be represented exactly as a (fixed width) decimal
number.

See the PDF at http://www.freescience.info/go.php?id=1663&pagename=books


>
>    if (f == 1.0f)
>       printf("f is 1.0\n");
>    else
>       printf("f is NOT 1.0\n");
>
>    return 0;
> }
> /*****************************/


-- 
PJH

http://shabbleland.myminicity.com/
http://www.chavgangs.com/register.php?referer=9375
http://www.kongregate.com/?referrer=Shabble

Reply via email to