[CentralOH] Plucking Digits From Float

Brian Costlow brian.costlow at gmail.com
Mon Dec 9 16:21:07 CET 2013


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).

http://docs.python.org/2/library/decimal.html

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:

> Hi,
>
> 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.)
>
> Joe
>
> 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
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20131209/849ce781/attachment.html>


More information about the CentralOH mailing list