473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how do you convert and array of doubles into floats?

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 3840
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
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
Marc 'BlackJack' Rintsch schrieb:
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)
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
In <4n************@uni-berlin.de>, Diez B. Roggisch wrote:
Marc 'BlackJack' Rintsch schrieb:
>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)

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
[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
Sep 15 '06 #6

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.

Sep 15 '06 #7
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
7
by: Asfand Yar Qazi | last post by:
Hi, I've got this class, you see: class Vec4 { Vec4(); ... float data; float& x;
14
by: my.correo.basura | last post by:
Yesterday I found in a piece of code a float being incremented with ++. For some reason (wrong, I guess...) I thought that only integer types could be incremented that way, and I had never seen...
3
by: Joakim Hove | last post by:
Hello, I have an array of doubles, allocated with dbarr = malloc(N * sizeof(double)); I then want to set all the elements of the array to zero, this is currently done with for (i=0; i <...
5
by: Macca | last post by:
Hi, My application receives data from multiple sources transferred over ethernet. This data is broken into packets of bytes before being transmitted over the network. My application has to...
12
by: Maxwell2006 | last post by:
Hi, I declared an array like this: string scriptArgs = new string; Can I resize the array later in the code? Thank you, Max
6
by: godavemon | last post by:
I need to take floats and dump out their 4 byte hex representation. This is easy with ints with the built in hex function or even better for my purpose def hex( number, size ): s =...
0
by: jeff_rowa | last post by:
Hi guyz, Could anyone please tell me how can I convert an array of floats to a string? I know that I can use this code to convert one float to a string but what is the best way for an array of...
9
by: John [H2O] | last post by:
Hello, I'm trying to do the following: datagrid = numpy.zeros(360,180,3,73,20) But I get an error saying that the dimensions are too large? Is there a memory issue here? So, my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.