function doubleToObject(num) {
var b = new ArrayBuffer(8),
f = new Float64Array(b),
i = new Uint32Array(b);
f[0] = num;
var whole = pad(i[1]) + pad(i[0]);
return {
sign: whole.slice(0, 1),
exponent: whole.slice(1, 12),
mantissa: whole.slice(12)
};
}
function pad(v) {
var s = v.toString(2),
n = 33 - s.length;
return new Array(n).join('0') + s;
}
function objectToDouble(obj) {
// I could just stuff the two halves into two ints but where is the fun in that
var bias = -1023;
console.log(obj)
var sign = obj.sign ? -1 : 1;
var mantissa = mantissa = parseInt("1" + obj.mantissa, 2) * Math.pow(2, -52);
if (obj.exponent === '00000000000') {
if (obj.mantissa !== '0000000000000000000000000000000000000000000000000000') {
// subnormal number detected!
var sigBitPos = obj.mantissa.indexOf('1');
console.log(sigBitPos, -(52 - sigBitPos - 1))
mantissa = parseInt("1" + obj.mantissa.slice(sigBitPos), 2) * Math.pow(2, -(52 - sigBitPos - 1)) ;
}
} else if (obj.exponent === "11111111111") {
return NaN;
}
var exponent = Math.pow(2, parseInt(obj.exponent, 2) + bias);
console.log(sign, exponent, mantissa);
return sign * (mantissa * exponent);
}
console.log(doubleToObject(NaN));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.