123.3 + 0.1 is 123.3999999999 ?

Grant Edwards grante at visi.com
Mon Jun 2 22:06:08 EDT 2003


In article <895e4ce2.0306021736.13c657a5 at posting.google.com>, D.W. wrote:

> 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...

Wha?   I defy you to find a system where 1 isn't stored exactly.

The more typical problem is with the inability to exactly represent 0.1
(base-10) as a base-2 FP number.

> 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.

Wha?

 1) It's a basic problem with storing FP numbers in one base (in a finite
    amount of memory) and displaying them in another.  It's not solvable,
    even in theory.
    
 2) Intel just followed the IEEE floating point standard. Though Intel ought
     to be thrashed for the awful IA32 architecture in general, Intel didn't
     do anything particulary wrong with floating point.

> 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.

There are a number of solutions.  Among them:

  1) Store it as a rational number (fraction).
  
  2) Store it in BCD fixed or floating point. Or, in a more general sense,
     store it in any base that can exactly represent base-10 numbers (if
     base-10 is what you care about).  

-- 
Grant Edwards                   grante             Yow!  Yow! And then we
                                  at               could sit on the hoods of
                               visi.com            cars at stop lights!




More information about the Python-list mailing list