Converting from Microsoft Binary Format floats to Python Float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • geskerrett@hotmail.com

    #1

    Converting from Microsoft Binary Format floats to Python Float

    In the '80's, Microsoft had a proprietary binary structure to handle
    floating point numbers, In a previous thread, Bengt Richter posted
    some example code in how to convert these to python floats;



    I copied this code and modified it slightly, however, you will notice
    that for one of the examples, the conversion isn't exact.

    Can anyone advise me on how to modify this code to correct for this
    situation ?
    I think the problem may be related to the different lengths of the
    mantissa. For double precision (8bytes) MBF format had 55 where as
    Python floats (IEEE) has only 52 ??

    Sample Code Below ----------------------
    # Conversion of Microsoft Binary Format numbers to Python Floats

    import binascii as bn
    import struct as st


    data = [(1234567890,'00 0000AF052C139F­ '),
    (4069954144,'00 000060929672A0' ­),
    (999999.99, '703D0AD7FF2374 94'),
    ( 88888.88, '400ad7a3709c2d 91'),
    ( 22222.22, '400ad7a3709c2d 8f'),
    ( 33333.33, 'b047e17a543502 90'),
    ( 1500.34, '7814ae47e18a3b 8b'),
    ( 42345.00, '00000000006925 90'),
    ]


    def msd2float(bytes ):
    #take out values that don't make sense possibly the NaN and
    Infinity ??
    if sum(bytes) in [0,72,127]:
    return 0.0
    b = bytes[:]
    sign = bytes[-2]&0x80
    b[-2] |= 0x80 #hidden most sig bit in place of sign
    exp = bytes[-1] - 0x80 - 56 #exponent offset
    acc = 0L
    for i,byte in enumerate(b[:-1]):
    acc |=(long(byte)<< (i*8))
    return (float(acc)*2.0 **exp)*((1.,-1.­)[sign!=0])

    for line in data:
    inval = line[0]
    binval = bn.unhexlify(li ne[1])
    le_bytes = list(st.unpack( 'BBBBBBBB',binv ­al))
    outval = msd2float(le_by tes)
    print " In:",inval, "\nOut:",outval ,"\n"

    Sample Output ------------------------
    C:/Python24/pythonw.exe -u "C:/pytest/dms/Test MBF.pyw"
    In: 1234567890
    Out: 1234567895.5

    In: 4069954144
    Out: 4069954144.0

    In: 999999.99
    Out: 999999.99

    In: 88888.88
    Out: 88888.88

    In: 22222.22
    Out: 22222.22

    In: 33333.33
    Out: 33333.33

    In: 1500.34
    Out: 1500.34

    In: 42345.0
    Out: 42345.0
    ----End Sample Output ----

  • Bengt Richter

    #2
    Re: Converting from Microsoft Binary Format floats to Python Float

    On 26 Aug 2005 07:55:26 -0700, geskerrett@hotm ail.com wrote:
    [color=blue]
    >In the '80's, Microsoft had a proprietary binary structure to handle
    >floating point numbers, In a previous thread, Bengt Richter posted
    >some example code in how to convert these to python floats;
    >
    >http://groups.google.com/group/comp....thread/42150c=
    >cc20a1d8d5/4aadc71be8aeddb e#4aadc71be8aed dbe
    >
    >I copied this code and modified it slightly, however, you will notice
    >that for one of the examples, the conversion isn't exact.
    >[/color]
    I suspect a typo in the hex ;-)
    [color=blue]
    >Can anyone advise me on how to modify this code to correct for this
    >situation ?
    >I think the problem may be related to the different lengths of the
    >mantissa. For double precision (8bytes) MBF format had 55 where as
    >Python floats (IEEE) has only 52 ??[/color]
    I believe that's 53 including the "hidden" msb. UIAM that means you
    could lose precision if the LS byte had bits below 'F8' -- i.e.,
    if bytes[0]&7 != 0 in the bytes passed to msd2float.

    But the bits that make the difference between 1234567890.0 and 1234567895.5
    are nowhere near there. Hence my suspicion of a typo.
    [color=blue]
    >
    >Sample Code Below ----------------------
    ># Conversion of Microsoft Binary Format numbers to Python Floats
    >
    >import binascii as bn
    >import struct as st
    >
    >
    >data =3D [(1234567890,'00 0000AF052C139F= AD'),[/color]
    [(1234567890,'00 0000A4052C139F= AD'),
    Are you sure it's not a 4? ----^
    [color=blue]
    > (4069954144,'00 000060929672A0' =AD),
    > (999999.99, '703D0AD7FF2374 94'),
    > ( 88888.88, '400ad7a3709c2d 91'),
    > ( 22222.22, '400ad7a3709c2d 8f'),
    > ( 33333.33, 'b047e17a543502 90'),
    > ( 1500.34, '7814ae47e18a3b 8b'),
    > ( 42345.00, '00000000006925 90'),
    > ]
    >
    >
    >def msd2float(bytes ):
    > #take out values that don't make sense possibly the NaN and
    >Infinity ??
    > if sum(bytes) in [0,72,127]:
    > return 0.0
    > b =3D bytes[:]
    > sign =3D bytes[-2]&0x80
    > b[-2] |=3D 0x80 #hidden most sig bit in place of sign
    > exp =3D bytes[-1] - 0x80 - 56 #exponent offset
    > acc =3D 0L
    > for i,byte in enumerate(b[:-1]):
    > acc |=3D(long(byte) <<(i*8))
    > return (float(acc)*2.0 **exp)*((1.,-1.=AD)[sign!=3D0])
    >
    >for line in data:
    > inval =3D line[0]
    > binval =3D bn.unhexlify(li ne[1])
    > le_bytes =3D list(st.unpack( 'BBBBBBBB',binv =ADal))
    > outval =3D msd2float(le_by tes)
    > print " In:",inval, "\nOut:",outval ,"\n"[/color]
    The above print will use str conversion for floats, so you are
    not seeing a full representation of the two values. (BTW, even the repr
    value is not necessarily an accurate representation of the exact
    decimal represented by the IEEE double's bits, but it is guaranteed
    to be converted back to those bits from the literal string representation. )
    E.g. (using my exact decimal hack to show the full value),
    [color=blue][color=green][color=darkred]
    >>> from ut.exactdec import ED
    >>> print .1[/color][/color][/color]
    0.1

    Which is the same as[color=blue][color=green][color=darkred]
    >>> print str(.1)[/color][/color][/color]
    0.1

    but repr shows that's not exact
    [color=blue][color=green][color=darkred]
    >>> print repr(.1)[/color][/color][/color]
    0.1000000000000 0001

    but the exact decimal representation for those bits is[color=blue][color=green][color=darkred]
    >>> print ED(.1, 'all')[/color][/color][/color]
    ED('0.100000000 000000005551115 123125782702118 158340454101562 5')
    [color=blue]
    >
    >Sample Output ------------------------
    >C:/Python24/pythonw.exe -u "C:/pytest/dms/Test MBF.pyw"
    > In: 1234567890
    >Out: 1234567895.5
    >[/color]
    try it with the typo fixed ;-)

    Regards,
    Bengt Richter

    Comment

    • geskerrett@hotmail.com

      #3
      Re: Converting from Microsoft Binary Format floats to Python Float

      Well, thank-you again.
      It's a bit embarassing, but you are correct ... It was a typo on in
      the sample data.
      A bit frustrated with myself as I checked, and double checked, but I
      guess became a bit blinded to the problem.

      Sorry to waste your time, and appreciate your assistance and patience.

      Geoff.

      Comment

      Working...