<div class="gmail_quote">On Tue, Apr 20, 2010 at 11:58 AM, Lowell Tackett <span dir="ltr"><<a href="mailto:lowelltackett@yahoo.com">lowelltackett@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I'm running headlong into the dilemma of binary math representation, with game-ending consequences, e.g.:<br>
<br>
>>> 0.15<br>
0.14999999999999999<br>
<br>
Obviously, any attempts to manipulate this value, under the misguided assumption that it is truly "0.15" are ill-advised, with inevitable bad results.<br>
<br>
the particular problem I'm attempting to corral is thus:<br>
<br>
>>> math.modf(18.15)<br>
(0.14999999999999858, 18.0)<br>
<br>
with some intermediate scrunching, the above snippet morphs to:<br>
<br>
>>> (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's help-aids that I apply to sort things out, such as formatting (%), or modules like "decimal" do nothing more than "powder up" the display for visual consumption (turning it into a string). The underlying float value remains "corrupted", 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('1.0')</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('0.15'), Decimal('18'))</div></div><div>
<br></div><div>Which are easily converted.</div><div><div>In [30]: (modf(modf(d)[0]*100)[0])/decimal.Decimal('.6')</div><div>Out[30]: Decimal('0.0')</div></div><div><br></div><div>So your problem with decimal isn't that it lacks the precision you're seeking - you'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>