how do you convert and array of doubles into floats?

Discussion in 'Python' started by SpreadTooThin, Sep 15, 2006.

  1. I have some code...

    import array

    a = array.array('d')
    f = open('file.raw')
    a.fromfile(f, 10)

    now I need to convert them into floats (32 bit...) what do i do?
     
    SpreadTooThin, Sep 15, 2006
    #1
    1. Advertising

  2. SpreadTooThin schrieb:
    > I have some code...
    >
    > import array
    >
    > a = array.array('d')
    > f = open('file.raw')
    > a.fromfile(f, 10)
    >
    > now I need to convert them into floats (32 bit...) what do i do?


    I guess module struct is your friend.

    Something like this:

    struct.pack("f" * len(a), *a)



    Diez
     
    Diez B. Roggisch, Sep 15, 2006
    #2
    1. Advertising

  3. In <>, SpreadTooThin
    wrote:

    > I have some code...
    >
    > import array
    >
    > a = array.array('d')
    > f = open('file.raw')
    > a.fromfile(f, 10)
    >
    > now I need to convert them into floats (32 bit...) what do i do?


    What about:

    b = array.array('f', a)

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Sep 15, 2006
    #3
  4. Marc 'BlackJack' Rintsch schrieb:
    > In <>, SpreadTooThin
    > wrote:
    >
    >> I have some code...
    >>
    >> import array
    >>
    >> a = array.array('d')
    >> f = open('file.raw')
    >> a.fromfile(f, 10)
    >>
    >> now I need to convert them into floats (32 bit...) what do i do?

    >
    > What about:
    >
    > b = array.array('f', a)


    AFAIK d and f are synonym for arrays, as python doesn't distinguish
    between these two on a type-level. And double it is in the end.

    Diez
     
    Diez B. Roggisch, Sep 15, 2006
    #4
  5. SpreadTooThin

    Tim Peters Guest

    [Marc 'BlackJack' Rintsch]
    >> What about:
    >>
    >> b = array.array('f', a)


    [Diez B. Roggisch]
    > AFAIK d and f are synonym for arrays, as python doesn't distinguish
    > between these two on a type-level. And double it is in the end.


    While Python has no type of its own corresponding to the native C
    `float`, the `array` and `struct` modules do understand the native C
    `float` . A Python float (same as a C `double`) gets converted to a C
    `float` when stored into one of those, and a C `float` is converted to
    a Python float (C `double`) when a value is extracted.

    >>> from array import array
    >>> x = 1.0000000001
    >>> x

    1.0000000001
    >>> array('d', [x])[0] # full precision is preserved

    1.0000000001
    >>> array('d', [x])[0] == x

    True
    >>> array('f', [x])[0] # trailing bits are lost

    1.0
    >>> array('f', [x])[0] == x

    False
     
    Tim Peters, Sep 15, 2006
    #5
  6. In <>, Diez B. Roggisch wrote:

    > Marc 'BlackJack' Rintsch schrieb:
    >> In <>, SpreadTooThin
    >> wrote:
    >>
    >>> I have some code...
    >>>
    >>> import array
    >>>
    >>> a = array.array('d')
    >>> f = open('file.raw')
    >>> a.fromfile(f, 10)
    >>>
    >>> now I need to convert them into floats (32 bit...) what do i do?

    >>
    >> What about:
    >>
    >> b = array.array('f', a)

    >
    > AFAIK d and f are synonym for arrays, as python doesn't distinguish
    > between these two on a type-level. And double it is in the end.


    No `array.array` is really about "C compiler types". You get C doubles in
    form of Python's `float` type if you read from the `array.array` but it's
    stored as C float and you can get the binary representation with the
    `tostring()` method.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Sep 15, 2006
    #6
  7. Tim Peters wrote:
    > [Marc 'BlackJack' Rintsch]
    > >> What about:
    > >>
    > >> b = array.array('f', a)

    >
    > [Diez B. Roggisch]
    > > AFAIK d and f are synonym for arrays, as python doesn't distinguish
    > > between these two on a type-level. And double it is in the end.

    >
    > While Python has no type of its own corresponding to the native C
    > `float`, the `array` and `struct` modules do understand the native C
    > `float` . A Python float (same as a C `double`) gets converted to a C
    > `float` when stored into one of those, and a C `float` is converted to
    > a Python float (C `double`) when a value is extracted.
    >
    > >>> from array import array
    > >>> x = 1.0000000001
    > >>> x

    > 1.0000000001
    > >>> array('d', [x])[0] # full precision is preserved

    > 1.0000000001
    > >>> array('d', [x])[0] == x

    > True
    > >>> array('f', [x])[0] # trailing bits are lost

    > 1.0
    > >>> array('f', [x])[0] == x

    > False


    The point being that when I say
    b.tofile(f) I expect the write to write 32 bit floats.
     
    SpreadTooThin, Sep 15, 2006
    #7
  8. >> AFAIK d and f are synonym for arrays, as python doesn't distinguish
    >> between these two on a type-level. And double it is in the end.

    >
    > No `array.array` is really about "C compiler types". You get C doubles in
    > form of Python's `float` type if you read from the `array.array` but it's
    > stored as C float and you can get the binary representation with the
    > `tostring()` method.


    I skimmed the docs of array and saw 'floating point' in both, assuming
    they were the same. Rereading them I actually recognized the "minimum
    size in bytes" column - and that is 4 for "f" and 8 for "double".

    If I were in "pardoning" mode, I'd say "but minimum doesn't exclude the
    possibility of floats having 8 bytes" - but I'm not :) So, I stand
    corrected.

    Thanks,

    Diez
     
    Diez B. Roggisch, Sep 16, 2006
    #8
    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. dan
    Replies:
    1
    Views:
    2,363
    Jack Klein
    Nov 26, 2003
  2. J.K. Becker
    Replies:
    43
    Views:
    1,045
  3. Andrew Reilly

    Re: Floats, doubles C and MSVC

    Andrew Reilly, Oct 14, 2004, in forum: C Programming
    Replies:
    2
    Views:
    703
    Tim Prince
    Oct 14, 2004
  4. Replies:
    14
    Views:
    571
    August Karlstrom
    May 26, 2005
  5. Pavel
    Replies:
    13
    Views:
    948
    Richard Herring
    Mar 14, 2008
Loading...

Share This Page