Bit twiddling floating point numbers

Jeff.Goldfinkle at gmail.com Jeff.Goldfinkle at gmail.com
Wed Mar 5 15:25:35 EST 2008


Hi All

Is there a simple way to twiddle the bits of a float? In particular, I
would like to round my float to the n most significant bits.

For example - 0.123 in binary is 0.000111111
Rounding to 4 bits I get 0.0001.

I can pack and unpack a float into a long
e.g.
struct.unpack('I',struct.pack('f',0.123))[0]
but then I'm not sure how to work with the resulting long.

Any suggestions?



More information about the Python-list mailing list