Bug or is it me?

Sarangan Thuraisingham sarangan.thuraisingham at gmail.com
Sun Oct 30 04:15:12 UTC 2005


As you may know 0.1 = 1 X (10 ^-1) or 1e-1  and 0.01 = 1e-2, etc. So 
1.082e-15 will be something like 0.000000000000001082 which is approx = 
0. I think you are getting this result because of the precision of 
double. Try using float.

What happens when you use C printf statements like:
  printf( "x = %f", x );

About the loop stop early, doesnt make sense to me. You loop logic seems 
to be ok. May be the output is still in buffer. Try printing somethin to 
ouput before exiting.

Regards,
  Saru

Rogelio Nodal wrote:
> Hello List:
> 
>     Is this a bug?Or is it me?
> 
>     [code]
> 
>     /* Created by Anjuta version 1.2.4 */
>     /*    This file will not be overwritten */
> 
>    #include <iostream>
>    #include <fstream>
> 
>    using namespace std;
> 
>    int main()
>    {
>    
>    
>    
>     double x = 0;
>    
>    
>     x = -2.3;
>     while(x <= 2.1)
>     {
>         cout << x << endl;
>         x += 0.1;
>     }
>    
>     return (0);
>    }
>  
>     [code]
> 
> 
>     When I run this little program I get the following output:
> 
> 
>     -2.3
> -2.2
> -2.1
> -2
> -1.9
> -1.8
> -1.7
> -1.6
> -1.5
> -1.4
> -1.3
> -1.2
> -1.1
> -1
> -0.9
> -0.8
> -0.7
> -0.6
> -0.5
> -0.4
> -0.3
> -0.2
> -0.1
> 1.082e-15    ===> What the hell?
> 0.1
> 0.2
> 0.3
> 0.4
> 0.5
> 0.6
> 0.7
> 0.8
> 0.9
> 1
> 1.1
> 1.2
> 1.3
> 1.4
> 1.5
> 1.6
> 1.7
> 1.8
> 1.9
> 2       === Why did it stop here?It is supposed to stop at 2.1.
> 
> 
> 
> Thanks a lot for your time.
> 
> 
>     Rogelio.
> 
> 




More information about the ubuntu-users mailing list