I have a file containing a large number, N, of 32-bit floats. This file is created using numpys memmap function as follows:
mmoutput = np.memmap("filename", dtype='f4', mode='w+', offset=0, shape=N)
mmoutput[:] = my_floats
mmoutput.flush()
When I load these coefficients back in using numpy and sum them using:
mminput = np.memmap("filename", dtype="f4", mode='c', offset=0, shape=N)
mminput.sum()
I get the value 82435.047 (which is correct).
However, when I read in the floats using C's mmap as follows:
int fd = open("filename", O_RDONLY, 0);
float * coefs = (float*) mmap(NULL, sizeof(float) * N, PROT_READ, MAP_SHARED, fd, 0);
double sum = 0.0;
for (int i = 0; i < N; i++) sum += coefs[i];
The numbers sum to a different value: 82435.100.
Can someone help spot my error? Perhaps there is a difference between the way numpy writes its floats and C reads them?
Full disclosure
I was actually just calculating the sum of these numbers as a check they are the same. The real use of them is as coefficients in a bspline (implemented using the einspline library as shown, for example, here https://github.com/ahay/src/blob/master/user/cram/esc_slow2.c). When I evaluate the splines in python and C I get different values.