News for hackers. Stuff that matters.
Re:The secret life of NaN by stcredzero on Thursday April 05, 2018 @03:52PM (#16768310)
Could this be used to hide data in Javascript programs?
Re:The secret life of NaN by mikekchar on Friday April 06, 2018 @02:51AM (#16771418)
As per TFA, some implementations use NaN-boxing to represent other values (all values are encoded into NaN). In those implementations, you will have pretty strange behaviour. V8 doesn't use NaN-boxing so it will work there.

Of course NaN-boxing is the reason that the ECMA spec specifies that all NaNs are treated equally. If you are encoding all of your values in NaN, you still have to have one NaN value that is actually NaN. The spec doesn't specify which one that must be.

BTW, I highly recommend reading TFA and the links within, because it's really fascinating (the "tagged-pointers" used in V8 and other languages like Guile are also really interesting).

Re:The secret life of NaN by scarmig on Thursday April 05, 2018 @06:46PM (#16769467)
You'd be relying on undefined behavior by the ECMAScript spec, but sure. Whether it works is implementation dependent, and you'd have no grounds for complaint if it changed at any time.

Playing around with it in Chrome 65.0.*...

  const buffer = new ArrayBuffer(12);
  const uint8s = new Uint8Array(buffer);
  const floats = new Float32Array(buffer);

  floats[0] = NaN, floats[1] = NaN; //127.192.0.0, 127.192.0.0, 0.0.0.0
  uint8s[0] = 104, uint8s[1] = 105; //127.192.104.105, 127.192.0.0, 0.0.0.0
  floats[2] = floats[0] + floats[1]; //127.192.104.105, 127.192.0.0, 127.192.104.105

  console.log(String.fromCharCode(uint8s[8]) + String.fromCharCode(uint8s[9]));
Re:The secret life of NaN by IshKebab on Thursday April 05, 2018 @04:21PM (#16768503)
Unlikely because those bits are use for type tagging.