<div dir="ltr">The (very few) hobby things I have done return ints of some type from the hardware, but it's not something I'm terribly familiar with.<div><br></div><div>And that was an off-the-top-of-my-head starting point. There's a couple issues with my code, including an unnecessary str conversion and not setting the context correctly.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Dec 9, 2013 at 10:35 AM, Joe Shaw <span dir="ltr"><<a href="mailto:joe@joeshaw.org" target="_blank">joe@joeshaw.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
Yep, good call.  I wasn't familiar with the Decimal library and hadn't<br>
caught up to your other message on the thread. It definitely looks<br>
like it'll fit the bill.<br>
<br>
I would be a bit surprised if the hardware returned floats... most<br>
small embedded components don't have FPUs or IEEE-754 implementations.<br>
 (And for things like medical devices, they'd more likely use some<br>
fixed point implementation to avoid these kinds of rounding errors.)<br>
<span class="HOEnZb"><font color="#888888"><br>
Joe<br>
</font></span><div class="im HOEnZb"><br>
On Mon, Dec 9, 2013 at 10:21 AM, Brian Costlow <<a href="mailto:brian.costlow@gmail.com">brian.costlow@gmail.com</a>> wrote:<br>
</div><div class="HOEnZb"><div class="h5">> There is an existing Decimal library in the standard library, which can be<br>
> used for a lot of this kind of work. It's designed to avoid these kind of<br>
> issues (mostly).<br>
><br>
> <a href="http://docs.python.org/2/library/decimal.html" target="_blank">http://docs.python.org/2/library/decimal.html</a><br>
><br>
> Although since he is working with hardware, if the boundary can only pass<br>
> floats into Python, he still will have potential issues. But using Decimal<br>
> you can actually trap instances of inexact conversion and deal with them,<br>
> instead of having them sneak by.<br>
><br>
><br>
><br>
><br>
><br>
><br>
> On Mon, Dec 9, 2013 at 9:57 AM, Joe Shaw <<a href="mailto:joe@joeshaw.org">joe@joeshaw.org</a>> wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> It's worth pointing out also that floating point values are an<br>
>> approximation.  This is one reason why you should never do direct<br>
>> comparisons with floating point numbers, but it could also bite you<br>
>> here.  It's possible that 1.2346 (or any value you choose) cannot be<br>
>> precisely represented as you wish, and so converting it to a string<br>
>> and taking the negative index might be very, very wrong.<br>
>><br>
>> 0.1 is one such number, and it can be demonstrated in python on some<br>
>> platforms.  Go into the Python REPL and type "0.1" and you might get<br>
>> "0.10000000000000001" back.<br>
>><br>
>> If this is really important you can store your values as integers --<br>
>> which are exactly precise -- and handle the decimals yourself.  (In<br>
>> other words, your own implemention of fixed point values.)<br>
>><br>
>> Joe<br>
>><br>
>> On Sun, Dec 8, 2013 at 2:40 PM,  <<a href="mailto:jep200404@columbus.rr.com">jep200404@columbus.rr.com</a>> wrote:<br>
>> > On Sun, 8 Dec 2013 14:36:01 -0500, Louis Bogdan <<a href="mailto:looiebwv@gmail.com">looiebwv@gmail.com</a>><br>
>> > wrote:<br>
>> ><br>
>> >> num is calculated value 1.2346<br>
>> >> a=num(-1) is indicating the numeral "6" with negative indexing<br>
>> >> notation.<br>
>> >> b=num(-2) is indicating the numeral "4"   "         "<br>
>> ><br>
>> > I question the sanity of do so. Nonetheless.<br>
>> ><br>
>> > num = 1.2346<br>
>> > a = str(num)[-1]<br>
>> > b = str(num)[-2]<br>
>> ><br>
>> > print num, a, b<br>
>> ><br>
>> > _______________________________________________<br>
>> > CentralOH mailing list<br>
>> > <a href="mailto:CentralOH@python.org">CentralOH@python.org</a><br>
>> > <a href="https://mail.python.org/mailman/listinfo/centraloh" target="_blank">https://mail.python.org/mailman/listinfo/centraloh</a><br>
>> _______________________________________________<br>
>> CentralOH mailing list<br>
>> <a href="mailto:CentralOH@python.org">CentralOH@python.org</a><br>
>> <a href="https://mail.python.org/mailman/listinfo/centraloh" target="_blank">https://mail.python.org/mailman/listinfo/centraloh</a><br>
><br>
><br>
><br>
> _______________________________________________<br>
> CentralOH mailing list<br>
> <a href="mailto:CentralOH@python.org">CentralOH@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/centraloh" target="_blank">https://mail.python.org/mailman/listinfo/centraloh</a><br>
><br>
_______________________________________________<br>
CentralOH mailing list<br>
<a href="mailto:CentralOH@python.org">CentralOH@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/centraloh" target="_blank">https://mail.python.org/mailman/listinfo/centraloh</a><br>
</div></div></blockquote></div><br></div>