I am attempting to load a binary file containing floating-point values into an array using JavaScript. Here is my current method:
var mRequest = new XMLHttpRequest();
mRequest.open('GET', 'res/binary_float_data.bin');
mRequest.responseType = 'arraybuffer';
mRequest.onreadystatechange = function () {
if (mRequest.readyState === 4) {
// Obtain bytes
var buffer = mRequest.response;
var dataview = new DataView(buffer);
// Create buffer (4 bytes / float)
var mFloatArray = new Float32Array(buffer.byteLength / 4);
// Populate floats
for (var i = 0; i < mFloatArray.length; i++)
{
mFloatArray[i] = dataview.getFloat32(i * 4); // Every 4th byte
}
console.log("Loaded "+mFloatArray.length+" floats");
// Process mFloatArray
}
};
mRequest.send();
However, upon inspecting the minimum, maximum, and average values of mFloatArray, I notice that they are inaccurate. The expected values should be:
min: -0.0094
max: 0.0081
avg: 0.00013196
Instead, the values I am seeing are:
min: -3.3985008792505584e+38
max: 0
avg: NaN
I am certain that the binary file is correct. Is there an issue with how I am parsing the XMLHttpRequest?
EDIT: Including a snippet of the binary file in hexadecimal format:
0002980: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0002990: 0000 0000 0000 0000 0000 0000 55df 11bc ............U...
00029a0: afc5 13bc c0b2 15bc 4205 17bc a094 17bc ........B.......
00029b0: e3d4 17bc cb41 18bc f2e6 18bc 464d 19bc .....A......FM..
00029c0: bb94 18bc f6ca 16bc 29a5 14bc 0000 0000 ........).......
00029d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
EDIT 2: I generated the binary file using Matlab's "fwrite" command with precision set as 'float32'.