[Tutor] Loop over floating point values

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Dec 2 15:53:03 CET 2013


On 02/12/2013 12:27, Dave Angel wrote:
> On Mon, 2 Dec 2013 16:28:38 +1000, Amit Saha <amitsaha.in at gmail.com> wrote:
>> Indeed, that's a good point. Surprisingly, C does it just fine:
>
>
>> # include <stdio.h>
>
>
>> int main(int argc, char **argv)
>> {
>>   float x = 0.0;
>>   while(x<1)
>>     {
>>       x += 0.1;
>>       printf("%f\n", x);
>>     }
>
>
>>   return 0;
>> }
>
>
>> gives the following output:
>
>
>> 0.100000
>> 0.200000
>> 0.300000
>> 0.400000
>> 0.500000
>> 0.600000
>> 0.700000
>> 0.800000
>> 0.900000
>> 1.000000
>
> Fine???? The output is pretty,  but thoroughly wrong. There's an extra
> value at the end.
>

Exactly what I thought at first glance but I double checked, please take 
another look :)

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence



More information about the Tutor mailing list