[Tutor] Python 2.5.4 - error in rounding

Wayne Werner waynejwerner at gmail.com
Sun May 23 19:06:28 CEST 2010


On Sat, May 22, 2010 at 9:58 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> On Sun, 23 May 2010 12:19:07 am Wayne Werner wrote:
> > On Sat, May 22, 2010 at 7:32 AM, Steven D'Aprano
> <steve at pearwood.info>wrote:
> > > Why do people keep recommending Decimal? Decimals suffer from the
> > > exact same issues as floats,
> >
> > This is exactly incorrect! The Decimal operator offers /exact/
> > decimal point operations.
>
> Decimal is only exact for fractions which can be represented by a finite
> sum of powers-of-ten, like 0.1, just like floats can only represent
> fractions exactly if they can be represented by a finite sum of
> powers-of-two, like 0.5.
>
> Not only did I demonstrate an example of rounding error using Decimal in
> my post, but you then repeated that rounding error and then had the
> audacity to claim that it was "exact":
>

Decimal doesn't round - exact precision, not exact accuracy. Floating point
has neither reliable precision or accuracy, at least to certain extents.
Decimal, OTOH will perform exactly the same under the exact same
circumstances every time. No matter how many points of precision you go out
to, .3333 * 3 can -never- be equal to 1 (except for very large values of 3).
1/3 is a different number than .33333 repeating. It's close, getting closer
the further out you go, and once it reaches infinity then sure, it's
equivalent. But unfortunately computers are finite state machines and
therefore are not capable of expressing the rational number 1/3 in its
decimal equivalent.

This has nothing to do with the Decimal module which will always perform
reliably - you can count on Decimals to behave, precisely, but floats not so
much


> <snip>
> I'm not. You are misrepresenting Decimal as a panacea for all rounding
> issues, which it is not.
>

I never said anything about rounding, I only said it performed Decimal
calculations exactly which it does.


>  > >>> d = 1/3.0
> > >>> d*3
> > 1.0
>
> Curiously, floats perform that specific calculation better than Decimal.
> That shows that sometimes you can have *too much* precision for
> calculation. float rounds off the answer after just 17 decimal places
> (by memory), giving exactly 1, while Decimal (by default) rounds to 28
> places, leading to an error.
>
> Of course, there are other calculations where Decimal is more accurate:
>
> >>> sum(0.1 for i in range(10)) == 1
> False
> >>> sum(Decimal('0.1') for i in range(10)) == 1
> True
>
> This is only to be expected, because 0.1 is an exact power of ten, but
> not an exact power of two.
>
>
Which is exactly what I stated - that Decimals perform exact decimal
operations, and are thus more reliable than floating point calculations.
Converting fractions to decimals is a separate issue entirely, but one that
Decimals will at least behave reliably on, which is the definition of exact
that I was using.

If you want accurate representation of rational numbers, then of course like
you suggested the Fraction module is available.

-Wayne


>
> --
> Steven D'Aprano
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100523/0f4e154e/attachment.html>


More information about the Tutor mailing list