<div class="gmail_quote">On Sat, May 22, 2010 at 9:58 AM, Steven D&#39;Aprano <span dir="ltr">&lt;<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div class="im">On Sun, 23 May 2010 12:19:07 am Wayne Werner wrote:<br>
&gt; On Sat, May 22, 2010 at 7:32 AM, Steven D&#39;Aprano<br>
&lt;<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>&gt;wrote:<br>
&gt; &gt; Why do people keep recommending Decimal? Decimals suffer from the<br>
&gt; &gt; exact same issues as floats,<br>
&gt;<br>
&gt; This is exactly incorrect! The Decimal operator offers /exact/<br>
&gt; decimal point operations.<br>
<br>
</div>Decimal is only exact for fractions which can be represented by a finite<br>
sum of powers-of-ten, like 0.1, just like floats can only represent<br>
fractions exactly if they can be represented by a finite sum of<br>
powers-of-two, like 0.5.<br>
<br>
Not only did I demonstrate an example of rounding error using Decimal in<br>
my post, but you then repeated that rounding error and then had the<br>
audacity to claim that it was &quot;exact&quot;:<br></blockquote><div><br>Decimal doesn&#39;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&#39;s close, getting closer the further out you go, and once it reaches infinity then sure, it&#39;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.<br>

<br>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<br><br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div class="im"><br>
&lt;snip&gt;<br>
</div>I&#39;m not. You are misrepresenting Decimal as a panacea for all rounding<br>
issues, which it is not.<br><div class="im"></div></blockquote><div><br>I never said anything about rounding, I only said it performed Decimal calculations exactly which it does.<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div class="im">
&gt; &gt;&gt;&gt; d = 1/3.0<br>
&gt; &gt;&gt;&gt; d*3<br>
&gt; 1.0<br>
<br>
</div>Curiously, floats perform that specific calculation better than Decimal.<br>
That shows that sometimes you can have *too much* precision for<br>
calculation. float rounds off the answer after just 17 decimal places<br>
(by memory), giving exactly 1, while Decimal (by default) rounds to 28<br>
places, leading to an error.<br>
<br>
Of course, there are other calculations where Decimal is more accurate:<br>
<br>
&gt;&gt;&gt; sum(0.1 for i in range(10)) == 1<br>
False<br>
&gt;&gt;&gt; sum(Decimal(&#39;0.1&#39;) for i in range(10)) == 1<br>
True<br>
<br>
This is only to be expected, because 0.1 is an exact power of ten, but<br>
not an exact power of two.<br>
<div><div class="h5"><br></div></div></blockquote><div><br>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. <br>

<br>If you want accurate representation of rational numbers, then of course like you suggested the Fraction module is available.<br><br>-Wayne<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div><div class="h5">
<br>
--<br>
Steven D&#39;Aprano<br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br><div style="visibility: hidden; display: inline;" id="avg_ls_inline_popup"></div><style type="text/css">#avg_ls_inline_popup {  position:absolute;  z-index:9999;  padding: 0px 0px;  margin-left: 0px;  margin-top: 0px;  width: 240px;  overflow: hidden;  word-wrap: break-word;  color: black;  font-size: 10px;  text-align: left;  line-height: 13px;}</style>