449,402 Members | 1,099 Online
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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? Sep 15 '06 #1
7 Replies

 P: n/a 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 Sep 15 '06 #2

 P: n/a In <11**********************@i3g2000cwc.googlegroups. com>, 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 Sep 15 '06 #3

 P: n/a Marc 'BlackJack' Rintsch schrieb: In <11**********************@i3g2000cwc.googlegroups. com>, SpreadTooThin wrote: >I have some code...import arraya = 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 Sep 15 '06 #4

 P: n/a In <4n************@uni-berlin.de>, Diez B. Roggisch wrote: Marc 'BlackJack' Rintsch schrieb: >In <11**********************@i3g2000cwc.googlegroups. com>, SpreadTooThinwrote: >>I have some code...import arraya = 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 Sep 15 '06 #5

 P: n/a [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 arrayx = 1.0000000001x 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 Sep 15 '06 #6

 P: n/a 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 arrayx = 1.0000000001x 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. Sep 15 '06 #7

 P: n/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. 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 Sep 16 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.