I have a binary file containing 8 byte floats (doubles). When I read it in python using this code:
import array
d = array.array('d')
d.fromfile(open("foo", mode="rb"), 10)
print d
I get different results than this java code run on the same file:
DataInputStream is;
try {
is = new DataInputStream(new FileInputStream(FILE_NAME));
int n = 0;
while(n < 10) {
System.out.println(is.readDouble());
n++;
}
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
What am I doing wrong?
Here is the example outputs:
Java:
-6.519670308091451E91
-6.723367689137016E91
0.0
-6.519664091503568E91
1.2029778888642203E-19
1.2029778888642203E-19
1.2028455399662426E-19
-1.1421078747242632E217
2.2734939098318505E236
-3.494281168153119E125
Python:
array('d', [-1.504936576164858e-36, -5.878658489696332e-39, 0.0, -5.878658478748688e-39, 5.878658495170291e-39, 5.878658495170291e-39, -5.878655692573363e-39, -5.87865851296011e-39, 4.79728723e-315, 1.546036714e-314])
Here is the C program I'm using to generate the data:
#include <stdio.h>
double test_data[10] = {
-1.504936576164858e-36,
-5.878658489696332e-39,
0.0,
-5.878658478748688e-39,
5.878658495170291e-39,
.878658495170291e-39,
-5.878655692573363e-39,
-5.87865851296011e-39,
4.79728723e-315,
1.546036714e-314
};
int main() {
FILE * fp;
fp = fopen("foo", "wb");
if(fp != NULL) {
fwrite(test_data, sizeof(double), 10, fp);
fclose(fp);
}
return 0;
}