Binary representation of floating point numbers

Grant Edwards grante at visi.com
Tue Dec 6 17:51:03 EST 2005


On 2005-12-06, 63q2o4i02 at sneakemail.com <63q2o4i02 at 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.lang.python/browse_frm/thread/300e220394e2d841/33bc9b0d8174b038?lnk=st&q=python+int+to+binary&rnum=1#33bc9b0d8174b038

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            



More information about the Python-list mailing list