profile
viewpoint

Ask questionsNaN byte representation mysteriously changes

  • Version: 13.12.0, 12.16.1, not 10.19.0
  • Platform: MacOS
  • Subsystem: v8

This is low priority and almost certainly a bug in v8, and I'm not sure if they care to fix it. But this program has mysterious behaviour:

const printBytes = (name, n) => {
  const bytes = Buffer.allocUnsafe(8)
  bytes.writeDoubleBE(n, 0)
  console.log(name + ': ', bytes)
}

const value = NaN

// Or equivalently:
// const b = Buffer.from('7ff8000000000000', 'hex')
// const value = new DataView(b.buffer).getFloat64(b.byteOffset, false)

const unused = {x: 5, value} // If you comment out this line, the problem goes away.
printBytes('same keys   ', ({x: 5, value}).value)
printBytes('diff keys   ', ({y: 5, value}).value)
printBytes('without prop', ({value}).value)
printBytes('naked       ', value)

Now, all those print statements should print the same value. But instead, this is what happens:

$ node test.js
same keys   :  <Buffer ff f7 ff ff ff f7 ff ff>
diff keys   :  <Buffer 7f f8 00 00 00 00 12 34>
without prop:  <Buffer 7f f8 00 00 00 00 12 34>
naked       :  <Buffer 7f f8 00 00 00 00 12 34>

The problem goes away if:

  • You comment out const unused = ...
  • You swap {x:5, value} to {value, x:5}
  • You use a value that isn't NaN

🤷‍♀️

nodejs/node

Answer questions targos

I'm not sure we can consider this a bug. There are multiple valid binary representations for NaN. I don't know if the ECMAScript spec requires only one of them to be materialized when the value is put in a typed array.

useful!

Related questions

--max-http-header-size= is not allowed in NODE_OPTIONS hot 7
Crash with "req.handle.writev is not a function" on Socket.Writable.uncork hot 2
pkg-exports: "." errors without a specified `main` hot 2
shutdown ENOTCONN on TLS.Socket._final hot 2
HTTP/2 requests eventually start throwing NGHTTP2_ENHANCE_YOUR_CALM errors hot 2
stream.finished behaviour change hot 2
Assertion `(parser->current_buffer_len_) == (0)' failed hot 1
Node.js 12.10 throwing EPROTO on HTTPS request hot 1
Remove util.inherits usage internally? hot 1
[Bug] Node 10.1.0 TLS issue with ldap: Client network socket disconnected before secure TLS connection was established hot 1
ReferenceError: internalBinding is not defined hot 1
Incorrect timezone hot 1
HPE_INVALID_HEADER_TOKEN on http requests hot 1
HPE_INVALID_HEADER_TOKEN on http requests hot 1
crypto, bad decrypt hot 1
Github User Rank List