Skip to content

Instantly share code, notes, and snippets.

@dherman
Last active December 15, 2015 11:19
Show Gist options
  • Save dherman/5251949 to your computer and use it in GitHub Desktop.
Save dherman/5251949 to your computer and use it in GitHub Desktop.
a NaN isn't a NaN isn't a NaN
function bufferBytes(buffer) {
return [].map.call(new Uint8Array(buffer), function(x) {return x})
}
function nanBytes(nan) {
var a = new Float64Array(1);
a[0] = nan;
return bufferBytes(a.buffer);
}
var buffer1 = (new Uint8Array([ 0, 0, 0, 0, 0, 0, 248, 127 ])).buffer;
var buffer2 = (new Uint8Array([ 0, 0, 0, 0, 0, 0, 248, 255 ])).buffer;
var nan1 = NaN;
var nan2 = Math.sqrt(-1);
var nan3 = (new Float64Array(buffer1))[0];
var nan4 = (new Float64Array(buffer2))[0];
console.log(nanBytes(nan1));
console.log(nanBytes(nan2));
console.log(nanBytes(nan3));
console.log(nanBytes(nan4));
// Firefox:
// [ 0, 0, 0, 0, 0, 0, 248, 127 ]
// [ 0, 0, 0, 0, 0, 0, 248, 255 ]
// [ 0, 0, 0, 0, 0, 0, 248, 127 ]
// [ 0, 0, 0, 0, 0, 0, 248, 127 ]
// Chrome:
// [ 0, 0, 0, 0, 0, 0, 248, 255 ]
// [ 0, 0, 0, 0, 0, 0, 248, 127 ]
// [ 0, 0, 0, 0, 0, 0, 248, 255 ]
// [ 0, 0, 0, 0, 0, 0, 248, 255 ]
@isaacs
Copy link

isaacs commented Mar 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment