News for hackers. Stuff that matters.
Re:WTFJS? by mrcharles on Friday February 12, 2010 @05:24PM (#1121971)
NaN === NaN being false is extremely handy. If you are trying to track down a rogue NaN in some code, equating a variable against itself finds it immediately. This is also the same in most other languages (I use it all the time in C++).
Re:WTFJS? by rflrob on Friday February 12, 2010 @05:41PM (#1122022)
Is there an advantage to doing that as opposed to using isNaN()?
Re:WTFJS? by mnemonicsloth on Friday February 12, 2010 @05:56PM (#1122054)
IIRC, it's a speed hack in the IEEE floating point standard.

NaN can happen for a lot of different reasons. You might have divided by zero because your code is broken, or you might have divided by epsilon because your algorithm isn't stable on this problem. [1]

This test checks to make sure none of {f, g, x, y, intermediate_values} were ever NaN, wihtout having to test each one every step of the way.

    if (  f(x) == g(y)  )
Which is a big deal if it's the guard on an inner loop in some numerical code.

[1] I'm fuzzy on the details. Corrections solicited.

Re:WTFJS? by endtime on Friday February 12, 2010 @10:56PM (#1122506)
If it's just a debugging trick, though, then speed hacks seem less useful.
Re:WTFJS? by grayrest on Friday February 12, 2010 @11:51PM (#1122568)
isNaN() will return true for anything that can't be coerced into a number while !(x == x) will only be true for the NaN value itself.
Re:WTFJS? by natep on Friday February 12, 2010 @09:10PM (#1122389)
How do you know isNaN() doesn't use this fact? In Java, the body of isNaN(n) is simply 'return n!=n;'
Re:WTFJS? by cdavid on Friday February 12, 2010 @11:06PM (#1122518)
It depends on the implementation. In C, it is quite easy to have n != n optimized away when compilers have their compiler flags set on. That's why you should use isnan to test nan instead of any other techniques when using C or C++.
Re:WTFJS? by kilian on Friday February 12, 2010 @05:37PM (#1122009)
I didn't know that! very useful, thank you.
Re:WTFJS? by mrcharles on Saturday February 13, 2010 @12:37AM (#1122611)
No problem. At least once per game I end up having to track rogue NaNs. Learned this trick a while back and it has saved my bacon many a time.