News for hackers. Stuff that matters.
Re:Shipping Culture Is Hurting Us by cwyers on Tuesday February 17, 2015 @01:08AM (#9060903)
NaN literally means "not a number." Lots of things aren't numbers. The letter a is not a number. The square root of negative 1 is not a number (at least, not one representable in floating point math). a is not equal to the square root of negative 1.
Re:Shipping Culture Is Hurting Us by pcl on Tuesday February 17, 2015 @01:34AM (#9060954)
In practice, NaN as a literal means that the outcome of a mathematical statement is not expressible. So, the letter 'a' is not equal to NaN (with either two or three = signs). NaN, in other words, has a special meaning.

Dismayingly, JavaScript's isNaN() function diverges from this special meaning, and does something close to what you said -- it tests to see if something is at least almost a number. So isNaN(13/0) and isNaN('foo') both evaluate to 'true', whereas isNaN('1') and isNaN(42) both evaluate to 'false'.

Re:Shipping Culture Is Hurting Us by flomo on Tuesday February 17, 2015 @05:15AM (#9061460)
Javascript type coercions might make sense if you realize it was originally designed for a close integration with HTML forms using a simplified proto-DOM ("Level 0").

The idea was more like "isNaN(myForm.myField)"?

For all I know, the above might actually still work. But either way, that explains Javascript type conversion logic.

Re:Shipping Culture Is Hurting Us by kazinator on Tuesday February 17, 2015 @01:24AM (#9060935)
I don't understand your point. This is not a number:

    char *p = "abc";
Yet, this yields true:

    p == p
The square root of -1 is easily representable in floating math. You just need two floats to make a complex.
Re:Shipping Culture Is Hurting Us by cwyers on Tuesday February 17, 2015 @10:10AM (#9062588)
Okay, but while "abc" is not a number, it is also the string "abc". NaN is special in that all it tells you is that the value is not a number -- it doesn't tell you what it is. In order to return a true value from an equality test, it isn't enough to know that both values are not a number, you have to know what they actually are.
Re:Shipping Culture Is Hurting Us by kazinator on Tuesday February 17, 2015 @05:02PM (#9065387)
If it is not known what is in the p variable, then the variable is indeterminate; it has exactly the same status as a variable that has not been initialized. In this case, the behavior upon accessing the variable should be undefined.

I agree with making accesses of NaN-valued variables undefined behavior, so that not comparing equal is then a possible consequence of undefined behavior.

I don't agree with defining the unequal comparison as the required behavior. To define the behavior is tantamount to the recognition that a NaN is something: an object. A variable can have a defined value which is that something, and that value must obey the Law of Identity.

Re:Shipping Culture Is Hurting Us by hueving on Tuesday February 17, 2015 @01:35AM (#9060956)
The point is that you can have very different things that are not a number. It's not safe to say that multiple things that are not a number are equal.
Re:Shipping Culture Is Hurting Us by kazinator on Tuesday February 17, 2015 @04:56PM (#9065350)
The values of two expressions that evaluate the same variable are not "multiple things".