retrieve bitwise float representation

U

Ulrich Eckhardt

Hi!

I need to pack a floating point value into a vector of 32-bit unsigned
values in IEEE format. Further, I maintain a CRC32 checksum for integrity
checking. For the latter, I actually need the float as integral value.

What I currently do is this:

tmp = struct.pack("=f", f)
(i,) = struct.unpack("=L", tmp)

IOW, I pack and unpack the float using the struct module, which works.


What I'm wondering is whether there are any better or alternative ways to
achieve this, the overhead now seems enormous and unnecessary to me here.

Thank you!

Uli
 
M

Mark Dickinson

Ulrich Eckhardt said:
Hi!

I need to pack a floating point value into a vector of 32-bit unsigned
values in IEEE format. Further, I maintain a CRC32 checksum for integrity
checking. For the latter, I actually need the float as integral value.

[...]

You could try using frexp to extract the significand and exponent of
the float, and then pack those values directly into an integer,
following the IEEE 754 format. For example:

Python 2.6.2 (r262:71600, Jun 8 2009, 14:57:27)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from struct import pack, unpack
from math import frexp, copysign
x = -34.125
unpack('<L', pack('<f', x))[0] 3255336960L
m, e = frexp(abs(x))
(e+125 << 23) + int(m*2.**24) + (2**31 if x < 0.0 else 0)
3255336960L

The above formula works for most x that are exactly representable as
an IEEE single-precision value, but extra effort would be required to
make it work with nans, infinities, zeros or denormals.

I'm not sure whether this is any faster than the pack/unpack route,
though. Obviously, if you're going to convert many floats then the
2.**24 and 2**31 constants should be precalculated.

If your values aren't already exactly representable as IEEE
single-precision floats then you may have a problem with rounding: the
call to int() in the above would truncate, while the implicit
conversion from double to single precision involved in packing with
'f' is more likely to do a round-to-nearest.
 
P

Peter Otten

Ulrich said:
I need to pack a floating point value into a vector of 32-bit unsigned
values in IEEE format. Further, I maintain a CRC32 checksum for integrity
checking. For the latter, I actually need the float as integral value.

What I currently do is this:

tmp = struct.pack("=f", f)
(i,) = struct.unpack("=L", tmp)

IOW, I pack and unpack the float using the struct module, which works.


What I'm wondering is whether there are any better or alternative ways to
achieve this, the overhead now seems enormous and unnecessary to me here.

binascii.crc32() operates on strings, so you don't need to unpack.
If you have many float values array.array() should be more effective than
struct.pack().
1758053516
 
T

Tom K.

I need to pack a floating point value into a vector of 32-bit unsigned
values in IEEE format. Further, I maintain a CRC32 checksum for integrity
checking. For the latter, I actually need the float as integral value. ....
What I'm wondering is whether there are any better or alternative ways to
achieve this, the overhead now seems enormous and unnecessary to me here.

Numpy has support for this:

import numpy as np
a = np.arange(10.) # an array of floats
b = a.view(dtype=np.uint32)

print b
array([ 0, 0, 1072693248, 0, 1073741824,
0, 1074266112, 0, 1074790400, 0,
1075052544, 0, 1075314688, 0, 1075576832,
0, 1075838976, 0, 1075970048, 0],
dtype=uint32)

b[0]=5
print a
array([ 1.06099790e-313, 1.00000000e+000, 2.00000000e+000,
3.00000000e+000, 4.00000000e+000, 5.00000000e+000,
6.00000000e+000, 7.00000000e+000, 8.00000000e+000,
9.00000000e+000])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top