JSFiddle - React, Tailwind, and code Playground

by Jon-Carlos Rivera

JavaScript

function doubleToObject (num) {
    var b = new ArrayBuffer(8),
        f = new Float64Array(b),
        i = new Uint32Array(b);
    f[0] = num;

    var whole = binaryPad(i[1]) + binaryPad(i[0]);

    return {
        sign: whole.slice(0, 1),
        exponent: whole.slice(1, 12),
        mantissa: whole.slice(12)
    };
}

function binaryPad(v, l) {
    l = (l || 32) + 1;
    var s = v.toString(2),
        n = l - s.length;
    return new Array(n).join('0') + s;
}

function objectToDouble (obj) {
    var b = new ArrayBuffer(8),
        f = new Float64Array(b),
        i = new Uint32Array(b);

    var whole = obj.sign + obj.exponent + obj.mantissa;

    i[0] = parseInt(whole.slice(32), 2);
    i[1] = parseInt(whole.slice(0, 32), 2);

    return f[0];
}

function NaNAbuse () {
    var b = new ArrayBuffer(8),
        f = new Float64Array(b),
        i = new Uint32Array(b);

    var NaNPrefix = "011111111111";
    var i = 0;
    // Both FF and Crome use a mantissa of "1000000000000000000000000000000000000000000000000000"
    // to represent all NaN's in the engine.
    
    // Let's see what the other ones do..
    var whole = NaNPrefix + binaryPad(Math.random() * 4503599627370496, 52);

    i[0] = parseInt(whole.slice(32), 2);
    i[1] = parseInt(whole.slice(0, 32), 2);

    // Answer: Nothing notable!
    return f[0];
}

console.log(doubleToObject(12.209834), objectToDouble(doubleToObject(12.209834)));