Binary representation of floating point numbers

Mike Meyer mwm at mired.org
Tue Dec 6 17:55:40 EST 2005


63q2o4i02 at sneakemail.com writes:
> In float mode, the instrument returns a sequence of bits that are
> exactly the ieee754 number in the case of floats, or just the flags in
> the case of flags.  PyVisa, when set to float mode, will convert
> everything to float, because it is unaware apriori that one of the
> fields returned is actually intended to be used as binary flags.

You need to get PyVisa to return strings of bytes to you.

> Actually now that I read the very confusing manual, it looks like maybe
> the flags is returned as a decimal number, but it's not clear how this
> is returned in either ascii or float mode.  In any case, I think I will
> need to manipulate "native" numbers into binary representation.  Looks
> like I should figure out the struct module...

Struct is the right tool for the job. But it manipulates strings of
bytes. If worst comes to worst, you could take the float returned by
PyVisa, use struct.pack to turn it into a string of bytes, then
struct.unpack to treat it as an integer on which you can do bit
manipulations. That works so long as everyone agrees about all the
details of the floating point representation. But you'd be better off
reading bytes and using struct.unpack to get an integer from them.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list