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)));
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.