4 ms·
tl;dr: - NaN is a floating point number, and NaN != NaN by definition in the IEEE 754-2019 floating point number standard, regardless of the programming langua
by jraph 1y ago
tl;dr:
- NaN is a floating point number, and NaN != NaN by definition in the IEEE 754-2019 floating point number standard, regardless of the programming language, there's nothing JavaScript-specific here.
- In JS Number.isNaN(v) returns true for NaN and anything that's not a number. And in JS, s * n and n * s return NaN for any non empty string s and any number n ("" * n returns 0). (EDIT: WRONG, sée below)
- JKCalhoun 1y agoI always thought of NaN as more of the concept of not-a-number the way that infinity in math is not a specific value but the concept of some unbounded largest possible value. Therefore, trying to do math with either (for example: NaN/NaN or inf./inf.) was to try to pin them down to something tangible and no longer conceptual — therefore disallowed.
- badosu 1y agoYou can use some form of real extensions, e.g. the extended real line (+inf, -inf is often useful for programmers) or the projectively extended real line (+inf = -inf). This is not about infinity in math not being a _specific_ value, it can certainly be (the actual infinite instead of potential). It's simply about design and foresight, in my humble opinion.
- lifthrasiir 1y ago> And in JS, s * n and n * s return NaN for any non empty string s and any number n ("" * n returns 0). No? It is easy to verify that `"3" * 4` evaluates to 12. The full answer is that * converts its operands into primitives (with a hint of being number), and any string that can be parsed as a number converts to that number. Otherwise it converts to NaN.
- jraph 1y agoAh indeed, thanks for the correction, I edited message.