I have a JS array with floats like so [0.0028808217, -0.027968751, -0.029748825] and I transform it to a Float32 Buffer with the following function:
function toFloat32Buffer(array) {
return Buffer.from(new Float32Array(array).buffer)
}
The issue I'm facing is making the conversion from the Float32 Buffer back to a JS array, so I can use the same values as the original array.
I've tried
function toJSArray(buffer) {
const newBuffer = Buffer.from(buffer)
const floatArray = new Float32Array(newBuffer)
return Array.from(floatArray)
}
function toFloat32Buffer(arr) {
return Buffer.from(new Float32Array(arr).buffer)
}
const array = [0.0028808217, -0.027968751, -0.029748825]
const bufferFromFloat32 = toFloat32Buffer(array);
const array2 = toJSArray(bufferFromFloat32);
console.log('array', array)
console.log('array2', array2)
array and array2 have different values. How should I transform back the buffer to get the same values as the original array? I'm running this in NodeJS
bufferargument andgetFloat32method to retrieve each of the float values from the buffer withtruemeaning the values should be interpreted accordingly. Example untested idea example perhaps (adjust accordingly): pastebin.com/ZJTDr3i7