5 ms·
Agreed, although some things like: NaN === NaN // -> false Are pretty f*&^%$ up, no matter how you look at them Edit: well, apparently there is a non f&^%
by tambourine_man 9y ago
Agreed, although some things like:
NaN === NaN // -> false
Are pretty f*&^%$ up, no matter how you look at them
Edit: well, apparently there is a non f&^%$ up way to look at it:
https://news.ycombinator.com/item?id=14891810 https://news.ycombinator.com/item?id=14891810
- roywiggins 9y agoThat's just normal floating point behavior. NaN isn't equal to anything, not even itself. It's in the standard.
- tambourine_man 9y agoFloating point can be a bit mind bending. Still, something not being equal to itself almost pushes the barrier into philosophy.
- fmihaila 9y agoNaN is not a name for one thing. You could think of not a number as not any particular thing. There is no comparison with itself, because there is no it.
- goatlover 9y agoHow is isNaN() implemented?
- Gaelan 9y agofunction isNaN(x) { return x !== x } It’s a native browser function so it may not actually be done that way, but that’s one way you could do it.
- tambourine_man 9y agoWe really are deep in the realm of Philosophy.
- roywiggins 9y agoAs long as you can define what's going on rigorously, it's just mathematics :) The equality operator means just what I choose it to mean- neither more nor less, as Humpty Dumpty would say. Also fun: (NaN < x) should be false for every x, and (NaN > x) should be false for every x. It's not a number, so it's not less than or greater than any number. But then by process of elimination, NaN == x for every x. Which is obviously nonsense. So equality and comparison is completely "broken" anyway when you start comparing NaN to things.
- aisofteng 9y agoThat's part of IEEE 754.
- tambourine_man 9y agoI didn't know it was part of IEEE 754, thanks. So JS is not alone in this.
- xyclos 9y agoNaN is meant to represent a non-sensical mathematical operation (like divide by 0). One non-sensical mathematical operation is not the same as some other non-sensical mathematical operation (1/0 !== Infinity/Infinity)
- Pigo 9y agoThat's a good point, a lot of this seems like it can be explained with some context or just asking why you'd ever want to do that.
- tambourine_man 9y agoThat's a good explanation, thanks.
- justinpombrio 9y agoNor is it the same as the same non-sensical mathematical operator :-) 1/0 !== 1/0
- tambourine_man 9y agoThat's mad. In JS, 1/0 is Infinity, not Undefined. And Infinity is not equal to itself.
- gmiller123456 9y ago>NaN is meant to represent a non-sensical mathematical operation This is not a very good explanation. NaN is used to represent a nonsensical value. By your explanation (1/0 === 1/0) should be true since they represent the same invalid opperation. The fact that NaN != NaN is not meant to mean anything, it is merely defined that way to prevent a class of bugs from occurring. Anyone not familiar with the definition is correct to be confused by it. I'd think if the spec was designed today, this special case (hack) wouldn't be there and we'd have exceptions in its place.
- fny 9y agoThere's a really great SO on why this is an IEEE standard: https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values https://stackoverflow.com/questions/1565164/what-is-the-rati...
- tambourine_man 9y agoGreat discussion indeed. I was thinking along the same lines, the comparison may be meaningless, but “false” is not a good answer either, even though a Boolean is either true or false by definition and NotABool would be even more crazy. What a rabbit hole.