<div class="gmail_quote">On Tue, Apr 20, 2010 at 11:58 AM, Lowell Tackett <span dir="ltr">&lt;<a href="mailto:lowelltackett@yahoo.com">lowelltackett@yahoo.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

I&#39;m running headlong into the dilemma of binary math representation, with game-ending consequences, e.g.:<br>
<br>
&gt;&gt;&gt; 0.15<br>
0.14999999999999999<br>
<br>
Obviously, any attempts to manipulate this value, under the misguided assumption that it is truly &quot;0.15&quot; are ill-advised, with inevitable bad results.<br>
<br>
the particular problem I&#39;m attempting to corral is thus:<br>
<br>
&gt;&gt;&gt; math.modf(18.15)<br>
(0.14999999999999858, 18.0)<br>
<br>
with some intermediate scrunching, the above snippet morphs to:<br>
<br>
&gt;&gt;&gt; (math.modf(math.modf(18.15)[0]*100)[0])/.6<br>
1.6666666666664298<br>
<br>
The last line should be zero, and needs to be for me to continue this algorithm.<br>
<br>
Any of Python&#39;s help-aids that I apply to sort things out, such as formatting (%), or modules like &quot;decimal&quot; do nothing more than &quot;powder up&quot; the display for visual consumption (turning it into a string).  The underlying float value remains &quot;corrupted&quot;, and any attempt to continue with the math adapts and re-incorporates the corruption.<br>

</blockquote><div><br></div><div>That is not precisely correct - modf first converts decimal to a float and then applies the calculation. Try this instead:</div><div><br></div><div>def modf(mydecimal):</div><div>    num = decimal.Decimal(&#39;1.0&#39;)</div>

<div>    return (mydecimal%num, mydecimal//num)</div><div><br></div><div>On my machine this returns (with </div><div><div>In [18]: modf(d)</div><div>Out[18]: (Decimal(&#39;0.15&#39;), Decimal(&#39;18&#39;))</div></div><div>

<br></div><div>Which are easily converted.</div><div><div>In [30]: (modf(modf(d)[0]*100)[0])/decimal.Decimal(&#39;.6&#39;)</div><div>Out[30]: Decimal(&#39;0.0&#39;)</div></div><div><br></div><div>So your problem with decimal isn&#39;t that it lacks the precision you&#39;re seeking - you&#39;re simply converting it to a float /before/ performing the calculations, which makes turning it into a decimal in the first place pretty much useless.</div>

<div><br></div><div>HTH,</div><div>Wayne</div></div>