Bug or is it me?

Rogelio Nodal rnodal at pegasus.rutgers.edu
Sun Oct 30 03:59:58 UTC 2005


Sarangan Thuraisingham wrote:

> 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.
>>
>>
>
I have been doing C++ programming for quite a time. I expected your
answer, I knew that it had to do with that. Even though I'm not a 100%
master, at least loops simple like that I have mastered. Here is another
code that I used and not mater whether I use float or double it gives me
a weird result.


Here is the code:

[CODE]

> /* Created by Anjuta version 1.2.4 */
> /*    This file will not be overwritten */
>
> #include <stdio.h>
> #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;
>     }
>     
>     
>     cout << "=========\n\n";
>     
>     
>     float y = 0;
>     
>     y = -5.1;
>     while(y <= 5.4)
>     {
>         cout << y << endl;
>         y += 0.1;
>     }
>     
>     
>     cout << "=====\n";
>     cout << -0.1 + 0.1 << endl; // NOTE how here it gives you zero.
>     cout << "=====\n";
>     
>     return (0);
> }

[END OF CODE]

And here is the output:

[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.08247e-15
> 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
> =========
>
> -5.1
> -5
> -4.9
> -4.8
> -4.7
> -4.6
> -4.5
> -4.4
> -4.3
> -4.2
> -4.1
> -4
> -3.9
> -3.8
> -3.7
> -3.6
> -3.5
> -3.4
> -3.3
> -3.2
> -3.1
> -3
> -2.9
> -2.8
> -2.7
> -2.6
> -2.5
> -2.4
> -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.900003
> -0.800003
> -0.700003
> -0.600003
> -0.500003
> -0.400003
> -0.300003
> -0.200003
> -0.100003
> -2.51979e-06
> 0.0999975
> 0.199997
> 0.299997
> 0.399997
> 0.499997
> 0.599997
> 0.699997
> 0.799998
> 0.899998
> 0.999998
> 1.1
> 1.2
> 1.3
> 1.4
> 1.5
> 1.6
> 1.7
> 1.8
> 1.9
> 2
> 2.1
> 2.2
> 2.3
> 2.4
> 2.5
> 2.6
> 2.7
> 2.8
> 2.9
> 3
> 3.1
> 3.2
> 3.3
> 3.4
> 3.5
> 3.6
> 3.7
> 3.8
> 3.9
> 4
> 4.1
> 4.2
> 4.3
> 4.4
> 4.5
> 4.6
> 4.7
> 4.8
> 4.9
> 5
> 5.1
> 5.2
> 5.29999
> 5.39999
> =====
> 0
> =====


[END OF OUTPUT]


Notice how with float it goes even more off. The next thing I will try
is with C, maybe the problem is the c++ compiler.
Thanks a lot for your answer and time.Thank!

    -Rogelio.




More information about the ubuntu-users mailing list