[SPAM-Bayes] - Re: Converting IBM Floats..Help.. - BayesianFilter detected spam

Scott David Daniels Scott.Daniels at Acm.Org
Sat Mar 27 01:47:46 CET 2004


Ian Sparks wrote:

> You might find my weird little bits module helpful to take your floating
> point values in python apart in order to send them around.
> 
>      http://members.dsl-only.net/~daniels/bits.html
> <<
> 
> Could you demonstrate this?
No, this is more for the other direction.

from bits import msb, extract

def asIBM(v):
     """Only works for positive numbers, doesn't round"""
     assert v >= 0
     if 0 == v:
         return 8 * '\0'
     top = msb(v)
     expon = top // 4 + 1
     data = [chr(extract(v, n-8, n)) for n in
             range(expon*4, expon*4 - 7*8, -8)]
     data.insert(0, chr((expon + 64) & 127))
     return ''.join(data)

I haven't really tested the above, so you'd really better
check it carefully against some known values:

    asIBM(10.0625) or whatever else you know.

or whatever you know equivalents for.


-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list