On Thu, Oct 22, 2009 at 4:46 PM, Okan <[email protected]> wrote:

> 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;
>
>    if (f == 1.0f)
>       printf("f is 1.0\n");
>    else
>       printf("f is NOT 1.0\n");
>
>    return 0;
> }
> /*****************************/

Because comparing floats for equality like that using == operator won't work.

See http://c-faq.com/fp/fpequal.html

for more details on how to do this correctly.

-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
    If I were to divulge it, it would overturn the world."
               -- Jelaleddin Rumi

Reply via email to