Live data from Hacker News

NaN does not mean "I am not a number”

github.com

1–10 of 20 posts

Re: NaN does not mean "I am not a number”

#4
post #3

This may be correct, but why name it NaN if its a number. I thought we were meant to write readable code. Poor naming like this in the specs really does not help.

> This may be correct, but why name it NaN if its a number.

It is definitively not a number in the usual, mathematical sense -- hence the name, which comes from IEEE 754.

However, the isNumber function doesn't really check if things are numbers, it checks if they are of the Number type, which, potentially confusingly, includes things which are not numbers, including the accurately named value Not-a-Number (NaN).

Re: NaN does not mean "I am not a number”

#5
post #3

This may be correct, but why name it NaN if its a number. I thought we were meant to write readable code. Poor naming like this in the specs really does not help.

> This may be correct, but why name it NaN if its a number. It is definitively not a number in the usual, mathematical sense -- hence the name, which comes from IEEE 754. However, the isNumber function doesn't really check if things are numbers, it checks if they are of the Number type, which, potentially confusingly, includes things which are not numbers, including the accurately named value Not-a-Number (NaN).

So it's Not-a-Number but isNumber returns true?

That would imply then that isNumber is named incorrectly. So perhaps isNumber should read isNumericType?

Most of the time I want to test, is this a number I can use in a normal mathematical sense. As in, can I use it in subtraction, multiplication etc. I know now that I should use isFinite and isNumber, but wouldn't it be nice to have just one well named function for this?

Re: NaN does not mean "I am not a number”

#7
post #5

Earlier quoted context omitted.

> This may be correct, but why name it NaN if its a number. It is definitively not a number in the usual, mathematical sense -- hence the name, which comes from IEEE 754. However, the isNumber function doesn't really check if things are numbers, it checks if they are of the Number type, which, potentially confusingly, includes things which are not numbers, including the accurately named value Not-a-Number (NaN).

So it's Not-a-Number but isNumber returns true? That would imply then that isNumber is named incorrectly. So perhaps isNumber should read isNumericType? Most of the time I want to test, is this a number I can use in a normal mathematical sense. As in, can I use it in subtraction, multiplication etc. I know now that I should use isFinite and isNumber, but wouldn't it be nice to have just one well named function for th…

> So it's Not-a-Number but isNumber returns true?

NaN is not a number (the concept) but it is a Number (the type).

> That would imply then that isNumber is named incorrectly.

Arguably, it is Number (the type) that is named incorrectly (a type called Number that includes a value called Not-a-Number seems problematic.) Maybe "Number" should be called "MaybeNumber".

And, perhaps more importantly, the fact that JS doesn't have a convenient type membership operator for primitive types is problematic, as if there was a convenient parallel to "instanceOf" for primitive types -- or if "instanceOf" just worked for them -- then libraries wouldn't need to provide something like what underscores isNumber does at all, and then any isNumber function would probably test "is the argument a number" rather than "is the argument a Number".

Re: NaN does not mean "I am not a number”

#8
post #5

Earlier quoted context omitted.

> This may be correct, but why name it NaN if its a number. It is definitively not a number in the usual, mathematical sense -- hence the name, which comes from IEEE 754. However, the isNumber function doesn't really check if things are numbers, it checks if they are of the Number type, which, potentially confusingly, includes things which are not numbers, including the accurately named value Not-a-Number (NaN).

So it's Not-a-Number but isNumber returns true? That would imply then that isNumber is named incorrectly. So perhaps isNumber should read isNumericType? Most of the time I want to test, is this a number I can use in a normal mathematical sense. As in, can I use it in subtraction, multiplication etc. I know now that I should use isFinite and isNumber, but wouldn't it be nice to have just one well named function for th…

It's consistent with the names of other type-checking functions. isNumber checks if a value is an instance of the Number class just like isDate checks if a value is an instance of the Date class. If it were isNumericType, they'd get questions about why the function doesn't follow the existing naming convention.

The only reason this is confusing for people is because they aren't familiar with IEEE 754 floating-point numbers and their representations. But since floats are used in almost every programming language, it's something that any programmer should be familiar with. Once they learn exactly what NaN means in that context, then the confusion should disappear.

Re: NaN does not mean "I am not a number”

#9
tl;dr: Guy suggests isNumber(NaN) should be false. But it's true because NaN is part of floating points as defined by IEEE.

My own interpretation? While programmers shouldn't care so much about this particular kind of semantics (instead they should understand the theory behind floats and never have to run into this issue) it might be better just to call NaNs something else, like "undefined number" or something.

Re: NaN does not mean "I am not a number”

#10

tl;dr: Guy suggests isNumber(NaN) should be false. But it's true because NaN is part of floating points as defined by IEEE. My own interpretation? While programmers shouldn't care so much about this particular kind of semantics (instead they should understand the theory behind floats and never have to run into this issue) it might be better just to call NaNs something else, like "undefined number" or something.

> My own interpretation? While programmers shouldn't care so much about this particular kind of semantics (instead they should understand the theory behind floats and never have to run into this issue) it might be better just to call NaNs something else, like "undefined number" or something.

Or, better, call the JavaScript type that represents IEEE-754 Binary64 format -- which includes the union of three sets (finite numbers in a given range, two infinite numbers, and two kinds of Not-a-Number values) something other than "Number".

Post reply on HN