4 hundred quadrillonth?

AggieDan04 danb_83 at yahoo.com
Thu May 21 22:01:03 EDT 2009


On May 21, 5:36 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Thu, May 21, 2009 at 2:53 PM, Carl Banks <pavlovevide... at gmail.com> wrote:
> > On May 21, 2:05 pm, seanm... at 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.



More information about the Python-list mailing list