Observability of NaN distinctions — is this a concern?

Kevin Reid kpreid at google.com
Wed Mar 20 10:09:00 PDT 2013

I noticed Object.is being discussed recently, and this reminded me of a
concern in definition of equality predicates: that there is more than one
NaN value.

I see that the current draft (March 8, 2013) section 8.1.5 discusses this,
but it says that “to ECMAScript code, all NaN values are indistinguishable
from each other.”

Depending on what you mean by “ECMAScript code”, this may be false given
the Typed Arrays extension, which allows direct access to the bit-patterns
of float values (the Typed Arrays spec permits, but does not require,
replacing a NaN value with any other NaN value on read or write).

 In some browsers, namely current Safari and current Chrome (stable, not
beta), there are at least two distinct observable patterns (apparently one
for the NaN literal and propagation from operations on it, and one for
operations on numbers that are undefined).

Is this considered a problem?

Scruffy test case:

function convert(f) {
  var buf = new ArrayBuffer(8);
  var a = new Float64Array(buf);
  var b = new Uint8Array(buf);
  a[0] = f;
  var s = "";
  for (var i = 7; i >= 0; i--) {
    s += '0'+b[i].toString(16).substr(-2);
  return f + ' ' + s;
document.write(convert(0/0) + "<br>");
document.write(convert(Infinity*0) + "<br>");
document.write(convert(Infinity-Infinity) + "<br>");
document.write(convert(Math.pow(-1, 0.2)) + "<br>");
document.write(convert(Math.sqrt(-1)) + "<br>");
document.write(convert(Math.log(-1)) + "<br>");
document.write(convert(NaN) + "<br>");
document.write(convert(NaN*0) + "<br>");
document.write(convert(Math.sqrt(NaN)) + "<br>");
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130320/1ee03fd7/attachment.html>

More information about the es-discuss mailing list