123.3 + 0.1 is 123.4 if using a variable

D.W. dwblas at yahoo.com
Tue Jun 3 03:15:20 CEST 2003


Evidently, python has different rules for numbers entered in
interactive mode.  Try this little snippet or just using a float
variable.  It works both as a program and in interactive mode.

x = 123.3
print x
for j in range( 0, 3 ) :
    x += 0.1
    print x

Results:
123.3
123.4
123.5
123.6

Onward through the fog.
D.W.

A Puzzled User <kendear_nospam at nospam.com> wrote in message news:<3EC3BCCD.
3090605 at nospam.com>...
> In Python 2.2.2
> 
>  >>> float("123.4")+0.1
> 123.5
> 
>  >>> float("123.3")+0.1
> 123.39999999999999
> 
>  >>> float("123.1") + 1
> 124.09999999999999
> 
> how come there are these inaccuracies?
> 
> 
> --------------------------------
> P.S.
> I just tried in Perl
> 
>    print eval("123.3") + 0.1;
> 
> and it gives
> 123.4




More information about the Python-list mailing list