× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Python: Float infinite length (Precision float)

Utkarsh Upadhyay
Jul 08, 2015
<p>You can use the <a href="https://docs.python.org/2/library/decimal.html">Decimal</a> class provided by the standard library.</p> <p>From the docs:</p> <blockquote> <p>Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem:</p> </blockquote> <pre><code> &gt;&gt;&gt; from decimal import * &gt;&gt;&gt; getcontext().prec = 6 &gt;&gt;&gt; Decimal(1) / Decimal(7) Decimal('0.142857') &gt;&gt;&gt; getcontext().prec = 28 &gt;&gt;&gt; Decimal(1) / Decimal(7) Decimal('0.1428571428571428571428571429') </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31224980/Python:%20Float%20infinite%20length%20(Precision%20float)/31224998">Stack Overflow</a>.</p>
comments powered by Disqus