[CentralOH] Plucking Digits From Float
joe at joeshaw.org
Mon Dec 9 16:35:40 CET 2013
Yep, good call. I wasn't familiar with the Decimal library and hadn't
caught up to your other message on the thread. It definitely looks
like it'll fit the bill.
I would be a bit surprised if the hardware returned floats... most
small embedded components don't have FPUs or IEEE-754 implementations.
(And for things like medical devices, they'd more likely use some
fixed point implementation to avoid these kinds of rounding errors.)
On Mon, Dec 9, 2013 at 10:21 AM, Brian Costlow <brian.costlow at gmail.com> wrote:
> There is an existing Decimal library in the standard library, which can be
> used for a lot of this kind of work. It's designed to avoid these kind of
> issues (mostly).
> Although since he is working with hardware, if the boundary can only pass
> floats into Python, he still will have potential issues. But using Decimal
> you can actually trap instances of inexact conversion and deal with them,
> instead of having them sneak by.
> On Mon, Dec 9, 2013 at 9:57 AM, Joe Shaw <joe at joeshaw.org> wrote:
>> It's worth pointing out also that floating point values are an
>> approximation. This is one reason why you should never do direct
>> comparisons with floating point numbers, but it could also bite you
>> here. It's possible that 1.2346 (or any value you choose) cannot be
>> precisely represented as you wish, and so converting it to a string
>> and taking the negative index might be very, very wrong.
>> 0.1 is one such number, and it can be demonstrated in python on some
>> platforms. Go into the Python REPL and type "0.1" and you might get
>> "0.10000000000000001" back.
>> If this is really important you can store your values as integers --
>> which are exactly precise -- and handle the decimals yourself. (In
>> other words, your own implemention of fixed point values.)
>> On Sun, Dec 8, 2013 at 2:40 PM, <jep200404 at columbus.rr.com> wrote:
>> > On Sun, 8 Dec 2013 14:36:01 -0500, Louis Bogdan <looiebwv at gmail.com>
>> > wrote:
>> >> num is calculated value 1.2346
>> >> a=num(-1) is indicating the numeral "6" with negative indexing
>> >> notation.
>> >> b=num(-2) is indicating the numeral "4" " "
>> > I question the sanity of do so. Nonetheless.
>> > num = 1.2346
>> > a = str(num)[-1]
>> > b = str(num)[-2]
>> > print num, a, b
>> > _______________________________________________
>> > CentralOH mailing list
>> > CentralOH at python.org
>> > https://mail.python.org/mailman/listinfo/centraloh
>> CentralOH mailing list
>> CentralOH at python.org
> CentralOH mailing list
> CentralOH at python.org
More information about the CentralOH