123.3 + 0.1 is 123.3999999999 ?

S. Francis sp at m.isa.pain
Thu May 15 12:58:18 EDT 2003


>  >>> float("123.1") + 1
> 124.09999999999999
> 
> how come there are these inaccuracies?

Floating-point calculations are inaccurate by nature: e.g. ``.1'' 
looks very `round' because within a 10-base context, but not when the 
base switches to 2 : 1/2+1/4+1/8+1/16+1/32...

 >>> reduce(lambda x,y:x+y, [2**-p for p in range(1, 21)])
0.99999904632568359

 >>> .1
0.10000000000000001

It is a simple matter of intuitive expectations.





More information about the Python-list mailing list