struct, IEEE-754 and internal representation

Discussion in 'Python' started by ej, Nov 9, 2005.

  1. ej

    ej Guest

    I ran into something today I don't quite understand and I don't know all the
    nitty gritty details about how Python stores and handles data internally.
    (I think) I understand why, when you type in certain floating point values,
    Python doesn't display exactly what you typed (because not all decimal
    numbers are exactly representable in binary, and Python shows you the full
    precision of what is representable). For example:

    >>> 0.9

    0.90000000000000002

    and

    >>> 148.73

    148.72999999999999

    So, I think I've got a pretty good handle on what the struct module is all
    about. Let's take that number, 148.73, and use struct functions to look at
    the raw bit pattern of what would be in a 32-bit register using IEEE754
    float representation:

    >>> hex(unpack('L', pack('f', x))[0])

    '0x4314BAE1L'

    That is, the four bytes representing this are 0x43, 0x14, 0xBA, 0xE1

    Now let's go back the other way, starting with this 32 bit representation,
    and turn it back into a float:

    >>> unpack('>f', pack('BBBB', 0x43, 0x14, 0xBA, 0xE1))[0]

    148.72999572753906


    Hmmmm... Close, but I seem to be losing more the I would expect here. I
    initially thought I should be able to get back to at least what python
    previously displayed: 148.72999999999999

    I know there are 23 bits of mantissa in an IEEE-754, with a free '1'...

    >>> hex(14872999)

    '0xe2f1a7'

    Looks like it takes 6 * 4 = 24 bits to represent that as an int....

    I am starting to think my expectation is wrong...

    If that's true, then I guess I am confused why Python is displaying
    148.72999572753906 when you unpack the 4 bytes, implying a lot more
    precision that was available in the original 32-bits? Python is doing
    64-bit floating point here? I'm obviously not understanding something...
    help?

    -ej
     
    ej, Nov 9, 2005
    #1
    1. Advertising

  2. ej

    Robert Kern Guest

    ej wrote:

    > If that's true, then I guess I am confused why Python is displaying
    > 148.72999572753906 when you unpack the 4 bytes, implying a lot more
    > precision that was available in the original 32-bits? Python is doing
    > 64-bit floating point here? I'm obviously not understanding something...
    > help?


    Yes, Python uses C double precision floats for float objects.

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Nov 9, 2005
    #2
    1. Advertising

  3. On 2005-11-09, ej <> wrote:

    > If that's true, then I guess I am confused why Python is displaying
    > 148.72999572753906 when you unpack the 4 bytes, implying a lot more
    > precision that was available in the original 32-bits? Python is doing
    > 64-bit floating point here?


    Yes. C-Python "float" objects are of the C type "double" and
    use 64-bit IEEE-754 representation on all the common platforms
    I know about.

    --
    Grant Edwards grante Yow! .. or were you
    at driving the PONTIAC that
    visi.com HONKED at me in MIAMI last
    Tuesday?
     
    Grant Edwards, Nov 9, 2005
    #3
  4. ej

    Guest

    Use 'd' as the format character for 64-bit double precision numbers with struct.

    >>> x = 148.73
    >>> unpack("<d", pack("<d", x))[0] == x

    True
    >>> unpack("<f", pack("<f", x))[0] == x

    False

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDcohCJd01MZaTXX0RAgEKAJwNYt7Rb/yeaLle4c2XsbIpAoLVXACghpMo
    XpcFtakHKmBkf+H4svGrZ5A=
    =dw0q
    -----END PGP SIGNATURE-----
     
    , Nov 9, 2005
    #4
  5. ej

    ej Guest

    Ah! Well! That explains it. I started to suspect that but (obviously) did
    not know that. LOL

    Thanks for your prompt reply, Grant. :)

    -ej
     
    ej, Nov 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Auronc

    IEEE-754 double class??

    Auronc, Sep 1, 2004, in forum: C++
    Replies:
    0
    Views:
    458
    Auronc
    Sep 1, 2004
  2. Lionel B
    Replies:
    0
    Views:
    1,071
    Lionel B
    Sep 23, 2004
  3. Dale Huffman

    IEEE 754 floats

    Dale Huffman, Sep 14, 2004, in forum: Python
    Replies:
    5
    Views:
    508
    Robert Kern
    Sep 15, 2004
  4. Grant Edwards
    Replies:
    11
    Views:
    639
    Grant Edwards
    Jul 15, 2005
  5. Nick Maclaren

    Numerics, NaNs, IEEE 754 and C99

    Nick Maclaren, Jun 14, 2006, in forum: Python
    Replies:
    33
    Views:
    1,000
    Grant Edwards
    Jun 15, 2006
Loading...

Share This Page