123.3 + 0.1 is 123.3999999999 ?

D.W. dwblas at yahoo.com
Mon Jun 2 21:36:22 EDT 2003


Good article.  It is correct when it says that this happens in any
language.  I always thought it was common knowledge among programmers
that "1" could be stored as 0.99999999999999999...  It is also common
knowledge (although I don;t know why or even if it is true) that this
is a result from Intel's initial chip designs i.e. from the way that
they store numbers.  Hence the many commercial and some open-source
math packages that take care of this.

I don't know of any easy way to solve this except to round and convert
to a string if you are going to store the data.  That way, at least,
you will be sure of the number of significant digits and therefore the
reliability.

D.W.

> 
> The Python tutorial has the full story:
> 
> http://www.python.org/doc/current/tut/node14.html
> 
> > I just tried in Perl
> >
> >    print eval("123.3") + 0.1;
> >
> > and it gives
> > 123.4
> 
> Perl lies.  So does Python, if you ask it to:
> 
> >>> print eval("123.3") + 0.1
>  123.4
> >>> print float("123.3") + 0.1
> 123.4
> 
> </F>




More information about the Python-list mailing list