code not true?

Kragen Sitaker kragen at dnaco.net
Sun Apr 23 01:39:05 EDT 2000


In article <Pine.BSF.4.10.10004230013310.29461-100000 at chi.pair.com>,
Michal Wallace (sabren) <sabren at manifestation.com> wrote:
>> Floating point number on a binary computer are strange. They're not that
>> accurate. 0.07 * 100 != 7.0.
>
>Why is this the case? I mean.. I understand why decimals are hard to
>show in binary... But why can't we use variably long strings, or
>something? Is it just a speed issue?

In base 3, one third is 0.1.  Two thirds is 0.2.

If we convert these base 3 numbers into finite-length decimal
fractions, we get something like 0.3333333 and 0.6666667.  If we
multiply these numbers by three, we get 0.9999999 and 2.0000001.  You
can use more digits of precision if you want, but no finite number of
digits of precision will be enough to get exactly 1 and exactly 2
(which are mathematically the right answer to three times one third and
three times two thirds.)

Analogously, you can't take fractions like 0.2 and convert them to
binary and expect to get exactly 0.2, no matter how many bits you use.
0.5 is fine; 0.2, not.

That said, it *is* just a speed issue --- there are representations
other than floating point, like the ordered-pair rational-number
representation, that give the right answer but are slower to work
with.
-- 
<kragen at pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08.  Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>
The power didn't go out on 2000-01-01 either.  :)



More information about the Python-list mailing list