# how do you convert and array of doubles into floats?

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

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?

2. ### Diez B. RoggischGuest

> 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

3. ### Marc 'BlackJack' RintschGuest

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?

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

Ciao,
Marc 'BlackJack' Rintsch

Marc 'BlackJack' Rintsch, Sep 15, 2006
4. ### Diez B. RoggischGuest

Marc 'BlackJack' Rintsch schrieb:
> 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?

>
>
> 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
5. ### Tim PetersGuest

[Marc 'BlackJack' Rintsch]
>>
>> 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
6. ### Marc 'BlackJack' RintschGuest

In <>, Diez B. Roggisch wrote:

> Marc 'BlackJack' Rintsch schrieb:
>> 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?

>>
>>
>> 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

Tim Peters wrote:
> [Marc 'BlackJack' Rintsch]
> >>
> >> 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.

8. ### Diez B. RoggischGuest

>> 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