On May 21, 5:36 pm, Chris Rebert <c...@rebertia.com> wrote:
> On Thu, May 21, 2009 at 2:53 PM, Carl Banks <pavlovevide...@gmail.com> wrote:
> > On May 21, 2:05 pm, seanm...@gmail.com wrote:
> >> The explaination in my introductory Python book is not very
> >> satisfying, and I am hoping someone can explain the following to me:
>
> >> >>> 4 / 5.0
>
> >> 0.80000000000000004
>
> >> 4 / 5.0 is 0.8. No more, no less.
...
>
> The `decimal` module's Decimal type is also an option to consider:
>
> Python 2.6.2 (r262:71600, May 14 2009, 16:34:51)
> >>> from decimal import Decimal
> >>> Decimal(4)/Decimal(5)
>
> Decimal('0.8')

>>> Decimal(1) / Decimal(3) * 3
Decimal("0.9999999999999999999999999999")
>>> Decimal(2).sqrt() ** 2
Decimal("1.999999999999999999999999999")

Decimal isn't a panacea for floating-point rounding errors.  It also
has the disadvantage of being much slower.

It is useful for financial applications, in which an exact value for
0.01 actually means something.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to