Re: Bizarre output.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Srinath M wrote:

> Can someone tell me why the text, "I see it" is not displayed when this
> program is run?
>
> #include <stdio.h>
>                                                                        
>                                                                 
> int main(void)
> {
>   float a, b;
>                                                                        
>                                                                 
>   double d;
>                                                                        
>                                                                 
>   a = 0.;
>                                                                        
>                                                                 
>   b = 1.;
>                                                                        
>                                                                 
>   for (d  = 0.0; d < 0.011; d = d + 0.001)
>     {
>       printf("d = %f\n",d);
>                                                                        
>                                                                 
>       if (d == 0.009)
>         {
>           printf ("I see it\n");
>         }
>     }
>   return 0;
> }
>
Well, in my opinion you should normally not use "==" in conection with
float or double, because floating-point arithmetic is not exact (not
only with gcc). Besides 0.001 has not an exact representation as
floating point number ... Try
...
if(d > 0.008999 && d < 0.009001)
    printf(...);
...
antonio


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux