Really basic problem

John Machin sjmachin at lexicon.net
Mon Oct 8 06:02:24 EDT 2007


On 8/10/2007 7:39 PM, tomamil wrote:
> i know this example is stupid and useless, but that's not the answer
> to my question.
> here it goes:
> 
> status = 0.0
> for i in range(10):
>    status = status + 0.1
> [snip]

0.1 can not be represented exactly as a binary floating-point number. to 
see what is really happening, use repr(), e.g. like this:

 >>> t = 0.0
 >>> for i in range(10):
...     t += 0.1
...     print i, t, repr(t)
...
0 0.1 0.10000000000000001
1 0.2 0.20000000000000001
2 0.3 0.30000000000000004
3 0.4 0.40000000000000002
4 0.5 0.5
5 0.6 0.59999999999999998
6 0.7 0.69999999999999996
7 0.8 0.79999999999999993
8 0.9 0.89999999999999991
9 1.0 0.99999999999999989
 >>>

Read this: http://docs.python.org/tut/node16.html

HTH,
John



More information about the Python-list mailing list