how do you convert and array of doubles into floats?

S

SpreadTooThin

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

Diez B. Roggisch

SpreadTooThin said:
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
 
M

Marc 'BlackJack' Rintsch

SpreadTooThin said:
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
 
D

Diez B. Roggisch

Marc said:
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
 
T

Tim Peters

[Marc 'BlackJack' Rintsch]
[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
 
M

Marc 'BlackJack' Rintsch

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
 
S

SpreadTooThin

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

Diez B. Roggisch

AFAIK d and f are synonym for arrays, as python doesn't distinguish
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top