That code is ugly as hell. Use
function DoubleToIEEE(f) {
var buf = new ArrayBuffer(8);
var float = new Float64Array(buf);
var uint = new Uint32Array(buf);
float[0] = f;
return uint;
}
If you want an actual Array instead of a Uint32Array (shouldn't make a difference in the most cases), add an Array.from call. You can also reduce this to a oneliner by passing the value to the Float64Array constructor:
function DoubleToIEEE(f) {
// use either
return new Uint32Array(Float64Array.of(f).buffer);
return Array.from(new Uint32Array(Float64Array.of(f).buffer));
return Array.from(new Uint32Array((new Float64Array([f])).buffer));
}
The inverse would just write the inputs into the uint slots and return the float[0] value:
function IEEEToDouble(is) {
var buf = new ArrayBuffer(8);
var float = new Float64Array(buf);
var uint = new Uint32Array(buf);
uint[0] = is[0];
uint[1] = is[1];
return float[0];
}
which can be shortened to
function IEEEToDouble(is) {
return (new Float64Array(Uint32Array.from(is).buffer))[0];
}