473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Binary representation of floating point numbers

Hi,

I'm using python to run some lab equipment using PyVisa. When I read a
list of values from the equipment, one of the fields is 32 bits of
flags, but the value is returned as a floating point number, either in
ASCII format, or pure binary. In either case, since I'm using PyVisa,
it converts the number to a single precision floating point, and that's
what I have to work with.

The question is how do I recover the bits out of this floating point
value so I can read the flags represented here?

Also, it's little (or big?) endian. Whatever... how do I manipulate
the endianness?

thanks
Michael

Dec 6 '05 #1
10 4146
On 2005-12-06, 63*******@sneakemail.com <63*******@sneakemail.com> wrote:
I'm using python to run some lab equipment using PyVisa. When I read a
list of values from the equipment, one of the fields is 32 bits of
flags, but the value is returned as a floating point number, either in
ASCII format, or pure binary.
Wha? That doesn't make any sense. If it's 32 bits of flags,
then it's not a floating point number "in pure binary".

I suppose you could pretend the 32 bit chunk is a float and
print the resulting value, but that's just insane. And it
won't work. For some bit patterns, there's no way to go from
ASCII back to that same bit pattern. IOW, there is not a 1:1
mapping between bit patterns and the string representation, so
if that's what you've got, you're screwed.
In either case, since I'm using PyVisa, it converts the number
to a single precision floating point, and that's what I have
to work with.
It sounds like PyVisa is broken if it's treating someting as a
float when it isn't.
The question is how do I recover the bits out of this floating
point value so I can read the flags represented here?
Use the struct module.
Also, it's little (or big?) endian. Whatever... how do I
manipulate the endianness?


Again, the struct module.

--
Grant Edwards grante Yow! ... I have read the
at INSTRUCTIONS...
visi.com
Dec 6 '05 #2
63*******@sneakemail.com writes:
Hi,
I'm using python to run some lab equipment using PyVisa. When I read a
list of values from the equipment, one of the fields is 32 bits of
flags, but the value is returned as a floating point number, either in
ASCII format, or pure binary.
Value returned by *what*? Your equipment doesn't return floating
point numbers, it returns bytes. Those can be interpreted in any
number of ways. What you have to do is get those bytes into something
that lets you read the bits out of it. How you do that depends on how
you're getting the bytes.
In either case, since I'm using PyVisa,
it converts the number to a single precision floating point, and that's
what I have to work with.
If the conversion is anything but trivial, there's a fair chance the
information you're interested is destroyed by it.
The question is how do I recover the bits out of this floating point
value so I can read the flags represented here?
I'm not sure this is doable in Python. I'm not sure I *want* it to be
doable in Python. In C, you use a cast.
Also, it's little (or big?) endian. Whatever... how do I manipulate
the endianness?


On most processors, you don't. If you can, doing so will cause the OS
to come to a screeching halt on most of them. I don't know if anyone
still building boxes that treats the underlying platform as context to
the degree you're asking for here.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 6 '05 #3
Hi,

okay, let me be more concise. The lab equipment has two formatting
modes, ascii, and float.

In ascii mode, it returns strings that represent the numeric value, so
e.g. 3.14 is returned as '3.14'. PyVisa, when set to read ascii mode,
will convert these strings to float with "visa.read_values()". (easy
enough to do myself too with visa.read(), split(), and eval()).

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.

Secondarily, I had to set the instrument to return the bits in small
endian for it to read properly (I could aternately set PyVisa to swap
endianness for me). I may need to manipulate this.

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

Michael

Dec 6 '05 #4
Ok, I figured it out...

The only way to get the flags is as a float, either through an ascii
string or a true float. The value of the float, however, is
representable as 24 bits of normal binary.

So for example, the value returned is +4.608400E+04
which is really an int, 46084, which is more easily convertible to
binary.

So the question becomes how to convert an int to binary, which I found
here
http://groups.google.com/group/comp....bc9b0d8174b038

So problem solved (just need to implement it).

Michael

Dec 6 '05 #5
On 2005-12-06, 63*******@sneakemail.com <63*******@sneakemail.com> wrote:
The only way to get the flags is as a float, either through an
ascii string or a true float.
That's perverse.

Really.

Somebody needs to be slapped.
The value of the float, however, is representable as 24 bits
of normal binary.
OK, that should preserve a 1:1 mapping between strings/floats
and flag bit patterns. It's still sick, though.
So for example, the value returned is +4.608400E+04 which is
really an int, 46084, which is more easily convertible to
binary.
You really don't need to convert it to "binary". Just convert
it to an integer object.
So the question becomes how to convert an int to binary, which
I found here

http://groups.google.com/group/comp....bc9b0d8174b038


I doubt you actually want to do that. Just leave it as an
integer, and test for the bits you care about:

def bit(n):
return 1<<n

flags = int(half_assed_float_representing_the_flag_values)

if flags & bit(0):
print "underrange"
if flags & bit(1):
print "overrange"
if flags & bit(19):
print "bit 19 is set but nobody can hear me scream"
if flags & bit(23):
callThePolice()

or whatever.

--
Grant Edwards grante Yow! I feel better about
at world problems now!
visi.com
Dec 6 '05 #6
63*******@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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 6 '05 #7
Actually that's probably the easiest way. I may want to use shorter
variable names :)

thanks
michael

Dec 7 '05 #8
On Tue, 06 Dec 2005 22:51:03 -0000, Grant Edwards <gr****@visi.com> wrote:
On 2005-12-06, 63*******@sneakemail.com <63*******@sneakemail.com> wrote:
The only way to get the flags is as a float, either through an
ascii string or a true float.


That's perverse.

Really.

Somebody needs to be slapped.
The value of the float, however, is representable as 24 bits
of normal binary.


OK, that should preserve a 1:1 mapping between strings/floats
and flag bit patterns. It's still sick, though.
So for example, the value returned is +4.608400E+04 which is
really an int, 46084, which is more easily convertible to
binary.


You really don't need to convert it to "binary". Just convert
it to an integer object.
So the question becomes how to convert an int to binary, which
I found here

http://groups.google.com/group/comp....bc9b0d8174b038


I doubt you actually want to do that. Just leave it as an
integer, and test for the bits you care about:

def bit(n):
return 1<<n

flags = int(half_assed_float_representing_the_flag_values)

if flags & bit(0):
print "underrange"
if flags & bit(1):
print "overrange"
if flags & bit(19):
print "bit 19 is set but nobody can hear me scream"
if flags & bit(23):
callThePolice()

or whatever.

You could also make a "whatever" like
class Bitview(long): ... def __new__(cls, v, width=32):
... inst = long.__new__(cls, v)
... inst.width = width
... return inst
... def __getitem__(self, i):
... if isinstance(i, slice): return list(self)[i]
... if i>=self.width or i+self.width<0:
... raise IndexError, '%r has only %r bits' %(long(self), self.width)
... if i<0: i = i + self.width
... bit = 1<<i
... return self&bit and 1 or 0
... def __repr__(self): return "Bitview(int('%s', 2))"% str(self)
... def __str__(self): return ''.join(map(str, self))[::-1]
... bv11 = Bitview(11, 8)
bv11 Bitview(int('00001011', 2)) bv11[8] Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 9, in __getitem__
IndexError: 11L has only 8 bits list(bv11) [1, 1, 0, 1, 0, 0, 0, 0] bv11[::-1] [0, 0, 0, 0, 1, 0, 1, 1] bv11[-1] 0 bv11[-5] 1 bv11[-6] 0 bv11[:4] [1, 1, 0, 1] str(bv11) '00001011' repr(bv11) "Bitview(int('00001011', 2))" eval(repr(bv11)) == bv11 True import sys
Bitview(sys.maxint)

Bitview(int('01111111111111111111111111111111', 2))

Add niceties to taste ;-)
Regards,
Bengt Richter
Dec 7 '05 #9
That looks pretty cool. I'll try it out.

thanks
Michael

Dec 7 '05 #10
Hallöchen!

Sorry for the late response, but the subject didn't catch my
attention ...

Mike Meyer <mw*@mired.org> writes:
63*******@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.


PyVISA does so when calling the read_raw() method.
[...]

Struct is the right tool for the job.


Exactly.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus ICQ 264-296-646
Dec 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
2
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats;...
3
by: Tanuki | last post by:
Hi All: I encounter a programming problem recently. I need to read a binary file. I need to translate the binary data into useful information. I have the format at hand, like 1st byte = ID,...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
25
by: Gaurav Verma | last post by:
Hi, I want to convert a floating point number (or a decimal number say 123.456) into binary notation using a C program. Can somebody help me out with it? Thanks Gaurav --...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
7
by: Gary Baydo | last post by:
In an effort to write a simple rounding function, I wrote the following code out of curiosity. My question is, can I rely on the output to 'make sense'? As an added 'exercise' I tried to write...
5
by: gdarian216 | last post by:
im doing conversions with two's complement binary numbers and i have some questions. how can I convert -2047 into a base 2 complement number? I know how to read the binary but there has to be a...
17
by: Bill Cunningham | last post by:
I was wondering if someone could look this file over for me. It compiles correctly and prints the number 2 so I know fscanf is working. I am reading a text file and converting the text data...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.