On Nov 21, 11:48 pm, "Mark T" <[EMAIL PROTECTED]> wrote: > Here's some functions to get the binary representation of a float. Then > just manipulate the bits (an exercise for the reader): > > import struct > > def f2b(f): > return struct.unpack('I',struct.pack('f',f))[0] > > def b2f(b): > return struct.unpack('f',struct.pack('I',b))[0] > > >>> f2b(1.0) > 1065353216 > >>> hex(f2b(1.0)) > '0x3f800000' > >>> b2f(0x3f800000) > 1.0 > >>> b2f(0x3f800001) > 1.0000001192092896 > >>> b2f(0x3f7fffff) > > 0.99999994039535522
And it's worth noting that thanks to the way the floating-point format is designed, the 'bit-twiddling' is actually just a matter of adding or subtracting one from the integer representation above. This works all the way from zero, through subnormals, up to and including infinities. Mark (but not the same Mark) -- http://mail.python.org/mailman/listinfo/python-list