The article may be found below:
Since there is no page specifically for Python at the site, I thought I'd whip up this complement to the piece. Below is an example for the language. Note, that first half illustrates standard floating-point math and its associated issues. The second half uses the decimal module to alleviate them. The drawback, as always a cost in performance.
1 2 3 4 5 6 7 8 9 10 11 | Python 2.6.2 ...
>>>
>>> .1 + .2
0.30000000000000004
>>> .33*3
0.98999999999999999
>>> from decimal import Decimal
>>>
>>> Decimal('.1') + Decimal('.2')
Decimal('0.3')
|
Further Reading:
- The most commonly forwarded link on the subject is Sun's much more detailed, though accessible piece. Study this one and earn your black belt (1st Dan):
What Every Computer Scientist Should Know About Floating-Point Arithmetic
No comments:
Post a Comment