Determine how many decimal digits of a float are precise
<p>In my real-life problem, I multiply the size of a widget, with a <code>size_hint</code> which can be anything from <code>0.</code> to <code>1.</code>. Assuming the minimum size of a widget is <code>0</code> and the max size is <code>10,000</code>, on which digit should I expect the error to occur, when I multiply <code>size * size_hint</code>? </p>

<p>For example* on <code>0.1</code> the error occurs on the.. </p>

<pre><code>&gt;&gt;&gt; from decimal import Decimal
&gt;&gt;&gt; Decimal.from_float(.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
#                           ^ 
#                           |_ here 
</code></pre>

<p>.. 18th decimal digit.</p>

<p>On the other hand, the error below occurs on the 14th decimal </p>

<pre><code>&gt;&gt;&gt; 1001*.2
200.20000000000002
</code></pre>

<p><strong>Questions</strong>:   </p>

<ul>
<li>Is there a way to determine on which exactly decimal digit the error will occur? </li>
<li>Is there a difference between Python 2 and Python 3?</li>
</ul>

<p>Using decimals instead of floats is not a option, and that both <code>size</code> and <code>size_hint</code> are provided by the user.</p>

<hr>

<p>*<sub>  I used <a href="https://docs.python.org/3.5/library/fractions.html" rel="nofollow"><code>Fraction</code></a> since <code>&gt;&gt;&gt; 0.1</code> is displayed as.. <code>0.1</code> in the console, but I think this is related to how it's printed, not how it's stored. </sub></p>

<p>To answer the decimal to double-precision floating-point conversion part of your question...</p>

<p>The conversion of decimal fractions between 0.0 and 0.1 will be good to <a href="http://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/" rel="nofollow">15-16 decimal digits</a> (Note: you start counting at the first non-zero digit after the point.)</p>

<p>0.1 = 0.1000000000000000055511151231257827021181583404541015625 is good to 16 digits (rounded to 17 it is 0.10000000000000001; rounded to 16 it is 0.1).</p>

<p>0.2 = 0.200000000000000011102230246251565404236316680908203125 is also good to 16 digits.</p>

<p>(An example only good to 15 digits:</p>

<p>0.81 = 0.810000000000000053290705182007513940334320068359375)</p>

python floating-point precision significant-digits