title: Clarification on the Decimal type in Python
body: <p>Everybody knows, or at least, <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" rel="nofollow noreferrer">every programmer should know</a>, that using the <code>float</code> type could lead to precision errors. However, in some cases, an exact solution would be great and there are cases where comparing using an epsilon value is not enough. Anyway, that's not really the point.</p>
  <p>I knew about the <code>Decimal</code> type in Python but never tried to use it. It states that <a href="http://docs.python.org/2/library/decimal.html" rel="nofollow noreferrer">&quot;Decimal numbers can be represented exactly&quot;</a> and I thought that it meant a clever implementation that allows to represent any real number. My first try was:</p>
  <pre><code>&gt;&gt;&gt; from decimal import Decimal
  &gt;&gt;&gt; d = Decimal(1) / Decimal(3)
  &gt;&gt;&gt; d3 = d * Decimal(3)
  &gt;&gt;&gt; d3 &lt; Decimal(1)
  True
  </code></pre>
  <p>Quite disappointed, I went back to the documentation and kept reading:</p>
  <blockquote>
  <p>The context for arithmetic is an environment specifying precision [...]</p>
  </blockquote>
  <p>OK, so there is actually a precision. And the classic issues can be reproduced:</p>
  <pre><code>&gt;&gt;&gt; dd = d * 10**20
  &gt;&gt;&gt; dd
  Decimal('33333333333333333333.33333333')
  &gt;&gt;&gt; for i in range(10000):
  ...    dd += 1 / Decimal(10**10)
  &gt;&gt;&gt; dd
  Decimal('33333333333333333333.33333333')
  </code></pre>
  <p>So, <strong>my question is:</strong> is there a way to have a Decimal type with an infinite precision? If not, what's the more elegant way of comparing 2 decimal numbers (e.g. d3 &lt; 1 should return False if the delta is less than the precision).</p>
  <p>Currently, when I only do divisions and multiplications, I use the <code>Fraction</code> type:</p>
  <pre><code>&gt;&gt;&gt; from fractions import Fraction
  &gt;&gt;&gt; f = Fraction(1) / Fraction(3)
  &gt;&gt;&gt; f
  Fraction(1, 3)
  &gt;&gt;&gt; f * 3 &lt; 1
  False
  &gt;&gt;&gt; f * 3 == 1
  True
  </code></pre>
  <p>Is it the best approach? What could be the other options?</p>